Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function
- Function Get-FolderByPath{
- <#
- .SYNOPSIS Retrieve folders by giving a path
- .DESCRIPTION The function will retrieve a folder by it's
- path. The path can contain any type of leave (folder or
- datacenter).
- .NOTES Author: Luc Dekens (http://www.lucd.info/2012/05/18/folder-by-path/)
- .PARAMETER Path
- The path to the folder.
- This is a required parameter.
- .PARAMETER Path
- The path to the folder.
- This is a required parameter.
- .PARAMETER Separator
- The character that is used to separate the leaves in the
- path. The default is '/'
- .EXAMPLE
- PS> Get-FolderByPath -Path "Folder1/Datacenter/Folder2"
- .EXAMPLE
- PS> Get-FolderByPath -Path "Folder1>Folder2" -Separator '>'
- #>
- param(
- [CmdletBinding()]
- [parameter(Mandatory = $true)]
- [System.String[]]${Path},
- [char]${Separator} = '/'
- )
- process{
- if((Get-PowerCLIConfiguration).DefaultVIServerMode -eq "Multiple"){
- $vcs = $defaultVIServers
- }
- else{
- $vcs = $defaultVIServers[0]
- }
- foreach($vc in $vcs){
- foreach($strPath in $Path){
- $root = Get-Folder -Name Datacenters -Server $vc
- $strPath.Split($Separator) | %{
- $root = Get-Inventory -Name $_ -Location $root -Server $vc -NoRecursion
- if((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "vm"){
- $root = Get-Inventory -Name "vm" -Location $root -Server $vc -NoRecursion
- }
- }
- $root | where {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl]}|%{
- Get-Folder -Name $_.Name -Location $root.Parent -Server $vc
- }
- }
- }
- }
- }
- # Import modules or snapins
- $PowerCLI = Get-PSSnapin -Name VMware.VimAutomation.Core -Registered
- try
- {
- switch ($powercli.Version.Major) {
- {
- $_ -ge 6
- }
- {
- Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop
- Write-Host -Object 'PowerCLI 6+ module imported'
- }
- 5
- {
- Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction Stop
- Write-Warning -Message 'PowerCLI 5 snapin added; recommend upgrading your PowerCLI version'
- }
- default
- {
- throw 'This script requires PowerCLI version 5 or later'
- }
- }
- }
- catch
- {
- throw 'Could not load the required VMware.VimAutomation.Vds cmdlets'
- }
- # Ignore self-signed SSL certificates for vCenter Server (optional)
- Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings:$false -Scope User -Confirm:$false
- # Variables
- $VCenter = Connect-VIServer 'vcenter1.home.local' -ErrorAction Stop | Out-Null
- $Cluster = 'vCluster1'
- $Datastore = Get-Datastore | Sort-Object FreeSpaceGB | Select -Last 1
- $VMFolder = Get-FolderByPath -Path 'USA/OS - Windows'
- $Template = Get-Template -Name 'TEMPLATE_2012-R2_Standard_2016-02-18'
- $ExistingCustSpec = '2012-R2 Standard - VLAN-211 (10.224.211.0/24)'
- $PortGroup = 'VLAN-211 (10.224.211.0/24)'
- $CSV = "C:\Users\SucksAtPowerShell\Desktop\newguests.csv"
- Import-Csv $CSV | ForEach-Object {
- $GuestVM = $($_.ServerName).Trim().ToUpper()
- $GuestVMIP = $($_.IPV4Address).Trim()
- $GuestVMNetMask = $($_.SubNet).Trim()
- $GuestVMGateway = $($_.Gateway).Trim()
- $GuestVMDNS1 = $($_.DNS1).Trim()
- $GuestVMDNS2 = $($_.DNS2).Trim()
- #$TempCustSpec = Get-OSCustomizationSpec -Name $ExistingCustSpec | New-OSCustomizationSpec -Name $GuestVM -Type NonPersistent
- $TempCustSpec = Get-OSCustomizationSpec -Name $ExistingCustSpec | New-OSCustomizationSpec -Name $GuestVM -Type Persistent
- Get-OSCustomizationSpec $TempCustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode:UseStaticIP -IpAddress $GuestVMIP -SubnetMask $GuestVMNetMask -Dns $GuestVMDNS1,$GuestVMDNS2 -DefaultGateway $GuestVMGateway
- New-VM -Name $GuestVM -Template $Template -Location $VMFolder -OSCustomizationSpec $CustSpec -ResourcePool $Cluster -Datastore $Datastore #-RunAsync
- Get-VM $GuestVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $PortGroup -Confirm:$false -ErrorAction Stop
- Remove-OSCustomizationSpec $GuestVM -Confirm:$False
- Get-VM -Name $GuestVM | Start-VM -RunAsync
- }
- Disconnect-VIServer -Confirm:$false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement