Thanks so much.
I'm very close, the VMs are deploying, but the network customizations are all failing. Here is the code:
$esxName = "ESXHOST1.DOMAIN.local"
$template = "W2K8R2SP1"
$datastore = "IOMEGA"
$newVmList = "SRV01", "SRV02", "SRV03", "SRV04"
$custSpec = "W2K8R2SP1"
$location = "_Tobedeleted"
$taskTab = @{}
Connect-VIServer -Server vc1.DOMAIN.local -User DOMAIN\ADMINISTRATOR -Password MYPASSWORD
# Create all the VMs specified in $newVmList
foreach($Name in $newVmList) {
$taskTab[(New-VM -Name $Name -VMHost (Get-VMHost -Name $esxName) -Template $template -Datastore $datastore -OSCustomizationSpec $custSpec -Location $location -RunAsync).Id] = $Name
}
# Start each VM that is completed
$runningTasks = $taskTab.Count
while($runningTasks -gt 0){
Get-Task | % {
if($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success"){
Get-VM $taskTab[$_.Id] | Start-VM
$taskTab.Remove($_.Id)
$runningTasks--
}
elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){
$taskTab.Remove($_.Id)
$runningTasks--
}
}
Start-Sleep -Seconds 15
}
# START HERE
# Wait for OS Customization
Start-Sleep -Seconds 300
# Customize network
Get-NetworkAdapter -Name $Name | Set-NetworkAdapter -NetworkName VM2 -Confirm:$false] = $Name
get-vm -Name $Name | Get-VMGuestNetworkInterface -Guestuser Administrator -GuestPassword "MYPASSWORD" |?{$_.name -eq "Local Area Connection 3"} | set-vmguestnetworkinterface -Guestuser Administrator -GuestPassword "MYPASSWORD" -IPPolicy static -IP 192.168.1.155 -Netmask 255.255.255.0 -Gateway 192.168.1.1 -DNS 192.168.1.2,192.168.1.3]