Hello,
With a lot of help from Eco91 I have produced the script below. It deploys a series of VMs at the same time, waits for them to boot and perfom an OS Customization, then changes basic networking from within the OS and also changes the Network Description:
$esxName = "esxqac4s01.MYDOMAIN.local"
$template = "TEST_W2K8R2ENTSP1"
$datastore = "NFS_VM_Storage01"
$newVmList = @(
@{"Name" = "TESTSVR1"; "IP" = "10.244.186.136"; "Netmask" = "255.255.255.0"; "Gateway" = "10.244.186.1"; "DNS" = @("10.244.37.25", "10.244.37.26"); "NetworkName" = "VLAN186_QA"; },
@{"Name" = "TESTSRV2"; "IP" = "10.244.186.137"; "Netmask" = "255.255.255.0"; "Gateway" = "10.244.186.1"; "DNS" = @("10.244.37.25", "10.244.37.26"); "NetworkName" = "VLAN186_QA"; },
@{"Name" = "TESTSRV3"; "IP" = "10.244.186.138"; "Netmask" = "255.255.255.0"; "Gateway" = "10.244.186.1"; "DNS" = @("10.244.37.25", "10.244.37.26"); "NetworkName" = "VLAN186_QA"; },
@{"Name" = "TESTSRV4"; "IP" = "10.244.186.139"; "Netmask" = "255.255.255.0"; "Gateway" = "10.244.186.1"; "DNS" = @("10.244.37.25", "10.244.37.26"); "NetworkName" = "VLAN186_QA"; }
)
$custSpec = "TEST_W2K8R2ENTSP1"
$location = "_Tobedeleted"
$taskTab = @{}
Connect-VIServer -Server vvcqa01yyz.MYDOMAIN.local -User "MYDOMAIN\MYACCOUNT" -Password MYPASSWORD
# Create all the VMs specified in $newVmList
foreach($VM in $newVmList) {
$taskTab[(New-VM -Name $VM.Name -VMHost (Get-VMHost -Name $esxName) -Template $template -Datastore $datastore -OSCustomizationSpec $custSpec -Location $location -RunAsync).Id]=$VM.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 5
}
# START HERE
# Wait for OS Customization
Start-Sleep -Seconds 300
# Customize network
foreach($VM in $newVmList) {
Get-NetworkAdapter -VM $VM.Name | Set-NetworkAdapter -NetworkName $VM.NetworkName -Confirm:$false
Get-VM -Name $VM.Name | Get-VMGuestNetworkInterface -Guestuser "Administrator" -GuestPassword "R!m2009" | ? { $_.name -eq "Local Area Connection 4" } | Set-VMGuestNetworkInterface -Guestuser "Administrator" -GuestPassword "R!m2009" -IPPolicy static -IP $VM.IP -Netmask $VM.Netmask -Gateway $VM.Gateway -DNS $VM.DNS
}
I would now like to replace the $VMlist variable and instead put th eVM information from a .csv file. Any suggestions?