My first script that I ever wrote was a script to build VMs. I was the newest member on the team, and i was giving the task of building 50 VMs for a new project that was getting started. It was a very daunting task, due to the completion date to have these 50 VMs to be completed. So one of the other members of the team told me about PowerCLI, but didn’t have any knowledge about it. So I started to read about it, and got it installed. I started to run very simple commands, such as Get-Template just to see what it does. The more I got used to running these commands, I started to put together the script. So I started with getting all of the information that I needed to build the VM, the VM specs. So I put all of this information into a CSV file.
So now that I have all of the VM specs into a CSV file, I needed to import them into the script for it to use.
$vms = Import-CSV "C:\Scripts\NewVMs.csv"
So now we have the bases of a the new script. Now I need to assign some variables. So as seen above in the CSV file we have header information, Name, Template, Cluster, Datastore,
Customization, vCPU, Memory, Network, Harddrive, Harddrive2, Diskformat, and Location. This step might be able to be avoided now, but had problems with it before, so that is why I chose
to set it up this way.
#Assign Variables $VMName = $vm.Name $Template = Get-Template -Name $vm.Template $Cluster = $vm.Cluster $Datastore = Get-Datastore -Name $vm.Datastore $Custom = Get-OSCustomizationSpec -Name $vm.Customization $vCPU = $vm.vCPU $Memory = $vm.Memory $Network = $vm.Network $Location = $vm.Location
Now that we have the variables going into the script we need to do the actually work. So the command that enables the VM to be created is New-VM. With that we can pump the most of the
variables into the command New-VM, and a new VM will be generated.
New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -Location $Location -StorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom
That only uses a portion of the variables that we collected, so lets add some more of them to get a more complete VM. So we still have the number of vCPUs, amount of memory and the Network adapter setting.
$NewVM = Get-VM -Name $VMName $NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false $NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false
At this point we have the parts for a working script to build VMs. Now just to put it all together and we’ll have a script that can build any number of VMs based on the information placed in to the CSV file.
$vms = Import-CSV "C:\Scripts\NewVMs.csv" foreach ($vm in $vms){ #Assign Variables $Template = Get-Template -Name $vm.Template $Cluster = $vm.Cluster $Datastore = Get-Datastore -Name $vm.Datastore $Custom = Get-OSCustomizationSpec -Name $vm.Customization $vCPU = $vm.vCPU $Memory = $vm.Memory $Network = $vm.Network $Location = $vm.Location $VMName = $vm.Name #Where the VM gets built New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -Location $Location -StorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom Start-Sleep -Seconds 10 #Where the vCPU, memory, and network gets set $NewVM = Get-VM -Name $VMName $NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false $NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false }
In the next post we will go over how to assign the IPs, subnet mask, gateway, and update the VMware tools.
– Stuart
Find this and all of my scripts at https://github.com/NotesofaScripter/PowerCLI