Advertisement
Guest User

vmware_deploy_vm-template_from_csv

a guest
Feb 18th, 2016
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Function
  2. Function Get-FolderByPath{
  3.   <#
  4. .SYNOPSIS  Retrieve folders by giving a path
  5. .DESCRIPTION The function will retrieve a folder by it's
  6.   path. The path can contain any type of leave (folder or
  7.   datacenter).
  8. .NOTES  Author:  Luc Dekens (http://www.lucd.info/2012/05/18/folder-by-path/)
  9. .PARAMETER Path
  10.   The path to the folder.
  11.   This is a required parameter.
  12. .PARAMETER Path
  13.   The path to the folder.
  14.   This is a required parameter.
  15. .PARAMETER Separator
  16.   The character that is used to separate the leaves in the
  17.   path. The default is '/'
  18. .EXAMPLE
  19.   PS> Get-FolderByPath -Path "Folder1/Datacenter/Folder2"
  20. .EXAMPLE
  21.   PS> Get-FolderByPath -Path "Folder1>Folder2" -Separator '>'
  22. #>
  23.  
  24.   param(
  25.   [CmdletBinding()]
  26.   [parameter(Mandatory = $true)]
  27.   [System.String[]]${Path},
  28.   [char]${Separator} = '/'
  29.   )
  30.  
  31.   process{
  32.     if((Get-PowerCLIConfiguration).DefaultVIServerMode -eq "Multiple"){
  33.       $vcs = $defaultVIServers
  34.     }
  35.     else{
  36.       $vcs = $defaultVIServers[0]
  37.     }
  38.  
  39.     foreach($vc in $vcs){
  40.       foreach($strPath in $Path){
  41.         $root = Get-Folder -Name Datacenters -Server $vc
  42.         $strPath.Split($Separator) | %{
  43.           $root = Get-Inventory -Name $_ -Location $root -Server $vc -NoRecursion
  44.           if((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "vm"){
  45.             $root = Get-Inventory -Name "vm" -Location $root -Server $vc -NoRecursion
  46.           }
  47.         }
  48.         $root | where {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl]}|%{
  49.           Get-Folder -Name $_.Name -Location $root.Parent -Server $vc
  50.         }
  51.       }
  52.     }
  53.   }
  54. }
  55.  
  56. # Import modules or snapins
  57. $PowerCLI = Get-PSSnapin -Name VMware.VimAutomation.Core -Registered
  58.  
  59. try
  60. {
  61.  switch ($powercli.Version.Major) {
  62.  {
  63.  $_ -ge 6
  64.  }
  65.  {
  66.  Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop
  67.  Write-Host -Object 'PowerCLI 6+ module imported'
  68.  }
  69.  5
  70.  {
  71.  Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction Stop
  72.  Write-Warning -Message 'PowerCLI 5 snapin added; recommend upgrading your PowerCLI version'
  73.  }
  74.  default
  75.  {
  76.  throw 'This script requires PowerCLI version 5 or later'
  77.  }
  78.  }
  79. }
  80. catch
  81. {
  82.  throw 'Could not load the required VMware.VimAutomation.Vds cmdlets'
  83. }
  84.  
  85. # Ignore self-signed SSL certificates for vCenter Server (optional)
  86. Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings:$false -Scope User -Confirm:$false
  87.  
  88. # Variables
  89. $VCenter = Connect-VIServer 'vcenter1.home.local' -ErrorAction Stop | Out-Null
  90. $Cluster = 'vCluster1'
  91. $Datastore = Get-Datastore | Sort-Object FreeSpaceGB | Select -Last 1
  92. $VMFolder = Get-FolderByPath -Path 'USA/OS - Windows'
  93. $Template = Get-Template -Name 'TEMPLATE_2012-R2_Standard_2016-02-18'
  94. $ExistingCustSpec = '2012-R2 Standard - VLAN-211 (10.224.211.0/24)'
  95. $PortGroup = 'VLAN-211 (10.224.211.0/24)'
  96. $CSV = "C:\Users\SucksAtPowerShell\Desktop\newguests.csv"
  97.  
  98. Import-Csv $CSV | ForEach-Object {
  99.     $GuestVM = $($_.ServerName).Trim().ToUpper()
  100.     $GuestVMIP = $($_.IPV4Address).Trim()
  101.     $GuestVMNetMask = $($_.SubNet).Trim()
  102.     $GuestVMGateway = $($_.Gateway).Trim()
  103.     $GuestVMDNS1 = $($_.DNS1).Trim()
  104.     $GuestVMDNS2 = $($_.DNS2).Trim()
  105.     #$TempCustSpec = Get-OSCustomizationSpec -Name $ExistingCustSpec | New-OSCustomizationSpec -Name $GuestVM -Type NonPersistent
  106.     $TempCustSpec = Get-OSCustomizationSpec -Name $ExistingCustSpec | New-OSCustomizationSpec -Name $GuestVM -Type Persistent
  107.     Get-OSCustomizationSpec $TempCustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode:UseStaticIP -IpAddress $GuestVMIP -SubnetMask $GuestVMNetMask -Dns $GuestVMDNS1,$GuestVMDNS2 -DefaultGateway $GuestVMGateway
  108.     New-VM -Name $GuestVM -Template $Template -Location $VMFolder -OSCustomizationSpec $CustSpec -ResourcePool $Cluster -Datastore $Datastore #-RunAsync
  109.     Get-VM $GuestVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $PortGroup -Confirm:$false -ErrorAction Stop
  110.     Remove-OSCustomizationSpec $GuestVM -Confirm:$False
  111.     Get-VM -Name $GuestVM | Start-VM -RunAsync
  112.    
  113. }
  114.  
  115. Disconnect-VIServer -Confirm:$false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement