I just finished a script that handles the re-ip of the VM. Ran into many obstacles, some of which LucD had provided assistance.. another friend of mine as well that programs for a living, unlike admins and integrators...
First, how sure are you all adapters are the "local area connection" and not "local area connection 1" or some other #? The netsh command will fail unless you know the exact adapter for which you wish to configure the IP on. Got passed it by querying the ip stack's primary connection for the proper local area connection name in use, and creating a variable with the results that can be called later.
Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText ipconfig | select-string -pattern "Local Area Connection[ ]?[\d]*" | % { $_.Matches } | % {$_.Value} | new-variable LAC
$LAC1 = $LAC -replace ".$"
$LAC2 = '"' + $LAC1 + '"'
Then I used the variable here:
$netsh = "c:\windows\system32\netsh.exe interface ip set address $LAC2 static $IP $SNM $GW 1"
put into full action:
Function Set-WinVMIP ($VM, $IP, $SNM, $GW, $dns1, $dns2, $LAC2){
Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxx -ScriptType bat -ScriptText ipconfig | select-string -pattern "Local Area Connection[ ]?[\d]*" | % { $_.Matches } | % {$_.Value} | new-variable LAC
$LAC1 = $LAC -replace ".$"
$LAC2 = '"' + $LAC1 + '"'
$netsh = "c:\windows\system32\netsh.exe interface ip set address $LAC2 static $IP $SNM $GW 1"
Write-Host "Setting IP address for $VM..."
Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText $netsh
Write-Host "Setting IP address completed."
$netsh1 = "c:\windows\system32\netsh.exe interface ip set dns name=$LAC2 static $dns1"
Write-Host "Setting Primary DNS for $VM..."
Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText $netsh1
Write-Host "Setting Primary DNS completed."
$netsh2 = "c:\windows\system32\netsh.exe interface ip add dns name=$LAC2 $dns2"
Write-Host "Setting Secondary DNS for $VM..."
Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText $netsh2
Write-Host "Setting secondary DNS completed."
Connect-VIServer $vcenter
foreach ($_.name in (Import-Csv "$ENV:USERPROFILE\Desktop\vscripts\vm-ip.csv" -UseCulture) ) {
$VM = Get-VM $_.name
Set-WinVMIP $VM -GuestUser administrator -GuestPassword xxxxxxx $_.IP $SNM $GW $dns1 $dns2
}
Second, make sure DHCP servers are NOT handing out DHCP addresses on the network, because if your new vmxnet3 adapter picks up an address - and not the auto generated address resulting from NO DHCP addresses available - netsh will fail with the error "unable to configure DHCP services" I had to scratch my head on this a thousand times, but it is the case. Netsh will overlay the auto assigned IP that is auto configured when the VM powers up after the new adapter is in.
I did not have any problems with user account control with w2008R2 builds, just vista, I believe the policies for UAC may have been shut off not sure..
Then to verify - the most important part of this because you will find a handful of failures I guarantee PERIOD - this runs against the cluster you define as a variable and performs an IP config against all the VMs and pipes the output to a file on your desktop
$vcenter = "10.x.x.x"
$Cluster = "test"
##########################################################
##### connect to vCenter, you will be prompted for credentials
##########################################################
connect-viserver $vcenter
Get-VM -location $Cluster | Invoke-VMScript -ScriptType bat -ScriptText "netsh interface IP show config" -GuestUser administrator -GuestPassword xxxxx | export-Csv $ENV:USERPROFILE\Desktop\vscripts\verify_VM_IPconfig.csv -NoTypeInformation
Disconnect-VIServer
##### End of Script, That's all Folks!
I'm sharing this because I couldn't have done it without the help of others so it is only right..
There are some great ping verification scripts out there as well that you can easily modify and verify connectivity as well which I recommend