Deploy VMs using Templates and PowerCli script

$csv1 = import-csv C:\users\administrator\desktop\temp.csv
 $cluster = get-cluster “Production01”
 $custspec = Get-OSCustomizationSpec windows

$i = 0
 function vmdeploy()
 $fqdn = $csv1.hostname[$i] + “.”+ $csv1.domain[$i]

Get-OSCustomizationSpec windows | Set-OSCustomizationSpec -NamingScheme Fixed -NamingPrefix $csv1.hostname[$i]

Get-OSCustomizationSpec windows | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $csv1.ip[$i] -SubnetMask $csv1.subnet[$i] -DefaultGateway $csv1.gateway[$i] -Dns $csv1.dns1[$i],$csv1.dns2[$i]

New-VM -ResourcePool $cluster -Name $fqdn -Template $csv1.template[$i] -Datastore $csv1.datastore[$i] -DiskStorageFormat thick -OSCustomizationSpec $custspec

get-vm $fqdn | get-networkadapter | set-networkadapter -portgroup $csv1.portgroup[$i] -confirm:$false | set-networkadapter -StartConnected:$true -confirm:$false

get-vm $fqdn | set-VM -MemoryMB $csv1.ram[$i] -NumCpu $csv1.cpu[$i] -confirm:$false

Start-VM -VM $fqdn

for ($i -eq 0;$i -lt $csv1.hostname.length;$i++){
 write-host “VM Deployment completed for VM” $fqdn

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