PowerCLI: Changing a VM IP Address with Invoke-VMScript
One of the cmdlets that has been much improved in the recent version of PowerCLU 4 U1 is the Invoke-VMScript cmdlet,
This cmdlet runs a script or command inside the guest OS of each of the specified virtual machines. To run Invoke-VMScript, the user must have read access to the folder containing the virtual machine and a Virtual Machine.Interaction.Console Interaction privilege.
The virtual machines must be powered on and have PowerShell and VMware Tools installed.
Network connectivity to the ESX system hosting the virtual machine on port 902 must be present.
You must also have both the Host and guest credentials available.
One question asked of me recently was “Is there a way to set the IP address of some windows virtual machines with PowerCI ?”.
So with the power of the invoke-cmdlet in the following example you can see how you can change the ip address by using the netsh command inside the VM.
As a note, you no longer need PowerShell inside the guest OS for this cmdlet to work, as you can see below I am calling a batch command, you can also use this cmdlet against *nix machines to run shell commands.
This of course can easily be adjusted to read all ip addresses from a csv file or text file and apply them to multiple virtual machines if needed.
The script is as follows:
Function Set-WinVMIP ($VM, $HC, $GC, $IP, $SNM, $GW){
$netsh = "c:\windows\system32\netsh.exe interface ip set address ""Local Area Connection"" static $IP $SNM $GW 1"
Write-Host "Setting IP address for $VM..."
Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential $GC -ScriptType bat -ScriptText $netsh
Write-Host "Setting IP address completed."
}
Connect-VIServer MYvCenter
$VM = Get-VM ( Read-Host "Enter VM name" )
$ESXHost = $VM | Get-VMHost
$HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials for $ESXHost", "root", "")
$GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "")
$IP = "192.168.0.81"
$SNM = "255.255.255.0"
$GW = "192.168.0.1"
Set-WinVMIP $VM $HostCred $GuestCred $IP $SNM $GW
And to see it in action I shot a quick video of the script in action:
PowerCLI Invoke-VMScript cmdlet from Alan Renouf on Vimeo.
Further examples of this cmdlet can be found in Arnim’s great example here:
http://www.van-lieshout.com/2010/01/powercli-get-wmi-info-from-isolated-guests/
And Arne’s great script here:
http://ict-freak.nl/2010/01/20/update-linux-vms-with-powercli-thanks-to-invoke-vmscript/







about 1 month ago
Alan, did you also check out Set-VMGuestNetworkInterface?
about 1 month ago
Just tried that one and couldnt get it working, was getting an error with Get-VMGuestNetworkInterface: Unable to parse script output.
about 1 month ago
Al, you might also specify that only guest OS with powershell environment could be used against this command
about 1 month ago
Al , a great Script again .
Considering that Starting Windows 2008 , Powershell comes with the OS by default , This would be a great script .
about 1 month ago
Al,
I am playing around with some automation stuff and was getting the same “unable to parse script output” error as you were while using Get-VMGuestNetworkInterface. The issue was actually that when I had called Set-NetworkAdapter I didn’t pass -Connected:$true in that statement. If the VM network adapter is not connected, it will throw the “unable to parse script output”. As soon as I changed the vNIC settings on the VM to “Connected” my scripts worked just fine.
HTH,
Tim
about 1 month ago
Tim,
As far as I know what this actually does is use the same method as invoke-vmscript but pushes a pre-defined bat file to the machine and runs that, unfortunatly I do not have the same issue as you as my nic is attached !
I have mentioned this in the forums and it would seam a few other people have an issue with the bat file too.
Hopefully this will be resolved in a future release.
Alan