Changing VM IP Addresses with PowerCLI

Sometimes VMs need to have their IP changed. And sometimes you waited to long to change the IP and you can no longer access your VM. There could be any number of reasons for this to happen, however when it happens, you are stuck doing a “Console” from vCenter to get into your guest.
Don’t get me wrong, the console has gotten SIGNIFICANTLY better over the last few years, and some may even use it as a Remote Desktop(RDP) substitute. But for others, it can be annoying to have to use the Console to have to get into a VM. I know, First World Problems right?!

Never fear my fellow Noobs, you don’t have to use the Console if you want to change the IP of a VM. You can use PowerCLI! Specifically the two cmdlets we will use are Get-VMGuestNetworkInterface and Set-VMGuestNetworkInterface.

Quick Side-Note: Although these two cmdlets have been around for a while they have recently…like last 6 months or a year…have gotten much more user friendly. There used to be requirements about what kind of boxes you could run them from or against(like x86 or x64 type stuff). I don’t remember specifics but long story short… Essentially if you have issues with these cmdlets, install the latest version of PowerCLI and try again (Edit: I was told I may not have been specific enough here…if you have questions about this, check out these release notes. Or post a comment! http://www.vmware.com/support/developer/PowerCLI/PowerCLI51/powercli51-793510-releasenotes.html)

First let’s see what IP the VM is working with…

Notice three things about this first screen shot.

I specify the VM I want to run the command against (testcr)

I decide to supply a different set of credential than the ones I am running Powershell/PowerCLI with…

The output is several network interfaces on the guest

I knew my current credentials didn’t have permissions to the guest OS, so I provided different ones. This is important to know because you aren’t going to make changes in VMware or vCenter, you will be making changes to the guest OS and therefore need permissions to make those changes.

Also since it returned several network interfaces we will need to filter it down.

That looks like the one we want. Now we can just pipe it to Set-VMGuestnetworkInterface with our changes.

This is kind of long command so let me break it up a bit to make it more clear.

That looks much better. Because I am silly and occasionally like making things harder on myself, I used (Get-Credential) twice. I could have also easily setup a variable $cred=Get-Credential, then just supplied $cred for the parameter -GuestCredential.

I just threw this together pretty quick, so let me know if it isn’t clear or you have questions! Enjoy!