Friday, 29 November 2013

VMware: Deploy VMs from Template using PowerCLI

0 comments
Syntax :

1..<N> | Foreach {
 New-VM -vmhost <ESX_HOST> -Name <VM_NAME> -Template <TEMPLATE> -Datastore <DATASTORE> [-OtherOptions]
 } 

Where:

<N> is the number of VMs you need to deploy.
<ESX_HOST> is the host where you will deploy your VMs
<TEMPLATE> is the template from which your VMs will be created
<DATASTORE> is the datastore that will contain your VMs
[-OtherOptions] are additional options that can be used in conjunction with New-VM command.

As you can see it's really basic, but New-VM cmdlet is really powerful and it has a lot of options. To go through all on New-VM options:


Get-Help New-VM -Detailed


Here's how I used it today for deploying 6 VMs that will be Oracle RAC nodes.


Open a text editor and paste this code in it saving file with .ps1 extension.


1..2 | Foreach {
 New-VM -vmhost esx1.testdomain.com -Name RAC$_ -Template OL6 -Datastore datastore1
 }
 3..4 | Foreach {
 New-VM -vmhost esx2.testdomain.com -Name RAC$_ -Template OL6 -Datastore datastore2 -ResourcePool TestResourcePool
 }
 5..6 | Foreach {
 New-VM -vmhost esx3.testdomain.com -Name RAC$_ -Template OL6 -Datastore datastore3 | Start-VM
 }  


As you can see we are deploying 6 VMs, named RAC1 to RAC6 from a template called OL6.
RAC1 and RAC2 will be deployed on host esx1.testdomain.com, RAC3 and RAC4 on host esx2.testdomain.com and RAC5 and RAC6 on host esx3.testdomain.com.
RAC3 and RAC4 will be placed in a resource pool called TestResourcePool.
RAC5 and RAC6 will be powered on after deployment due to Start-VM cmdlet.