PowerShell:: PowerCLI:: Provision multiple VMs from a Template on Vmware

Below script create multiple VMs from the same template on same cluster and datastore

$noVms = Read-host “Enter number of VMs to be created”

$cluster = Read-host “Enter Name of the cluster”

$dsCluster = Read-host “Enter Name of the Datastore cluster”

$template = Read-host “Enter template name”

Connect-VIServer <vcenter server name> -Credential (Get-Credential -Message “Credential for vCenter to connect”)

for ($a=1; $a -le $noVms; $a++) {

$Cluster = Get-Cluster -name “$cluster”

$DatastoreCluster = Get-DatastoreCluster -name “$dsCluster”

$VMname = “Servername0″+$a

$VmHost = Get-Cluster -Name $Cluster | Get-VMHost | Sort-Object CpuUsageMhz | Select -First 1

$Datastore = Get-DatastoreCluster -Name $DatastoreCluster| Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1

New-VM -Name -Template $template -VMHost $VmHost -Datastore $Datastore -OSCustomizationSpec ServerDeploy

start-sleep 60

get-vm $VMname | start-vm



You can also use the below selection method to retrieve available cluster, template, datastore cluster using

$templates = Get-Template
$menu = @{}
for ($i=1;$i -le $templates.count; $i++)
{ Write-Host “$i. $($templates[$i-1].name)”
[int]$ans = Read-Host ‘Select appropriate template from above list’
$template = $menu.Item($ans)



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s