PowerShell Friday: Creating Virtual Machines
One of the most basic things you will do within a virtual infrastructure is creating virtual machines. Adding one new virtual machine like this is fine, but if you have to repeat this proces multiple times it might be more usefull to use a script containing the PowerCLI cmdlet New-VM.
You can use the New-VM cmdlet in conjuction with templates and customization specifications, which can also be created through the use of PowerCLI. Those cmdlets will be handled in articles yet to come.
There are a lot of parameters that can be used with the New-VM cmdlet but at the very least you should enter a virtual machine name and cluster / resourcepool.
New-VM -Name ‘VMname’ -ResourcePool ‘ResourcePool’
If you also want to specify the location of the VDMK files you can add the Datastore parameter
New-VM -Name ‘VMname’ -ResourcePool ‘ResourcePool’ -Datastore ‘Datastore’
When you want to clone a virtual machine you can add the “-VM” parameter
New-VM -Name ‘VMname’ -VM ‘VMtoClone’ -ResourcePool ‘ResourcePool’
To import a virtual machine you will need to first get the folder name on a specific datastore and load that into a variable. You can then use that variable together with the new-vm cmdlet to import a virtual machine
cd vmstores:\MYserver@443\Datacenter\Datastore\MYVirtualMachine\ $vmxFile = Get-Item ‘MyVirtualMachine.vmx’ $vmhost = Get-VMHost -Name ‘MyVMHost’ New-VM -VMHost $vmhost -VMFilePath $vmxFile.DatastoreFullPath
By default the New-VM cmlet will create a virtual machine that contains 1 vCPU, 256 MB memory and a 4GB disk and a NIC connected at “internal network”. To change this you can add parameters like this.
New-VM -Name ‘VMname’ -ResourcePool ‘ResourcePool’ -NumCpu 4 -MemoryMB 2048 -DiskMB 40960 -NetworkName ‘production network’
As mentioned there are a lot of parameters you can use and the above examples only contain about half. I won’t write them all out in detail, instead I will give a summation for the rest of the parameters and a description.