PowerCLI change network label for all VMs in a folder
Just a quick blurb and a useful one liner used to change all the Network labels for all virtual machines that existed in a vCenter folder. ย I had a situation in a lab where I imported numerous server VMs from a production network that would be used for development/testing. ย In vCenter these were brought in just with the default first network in the list. ย This process is cumbersome to do if you are simply manually editing the properties of each VM and changing the network label/port group. ย In steps PowerCLI. ย Let’s see how to use PowerCLI change network label for all VMs in a folder.
PowerCLI change network label for all VMs in a folder
To do this we want to first connect into vCenter. ย If you connect to an individual host within your environment, using the connect-viserver commandlet, you will not be able to target “folders” in your -location parameter as only in vCenter do you have visibility into folders. ย So first let’s connect to vCenter:
connect-viserver yourvcenter.somedomain.com
After we have connected to vCenter, let’s use the one liner we mentioned about to pull out the networkname of the VMs in a particular folder and then set the network adapter network name to a networkname of our choosing.
get-vm -location "MYFOLDER" | get-networkadapter | where {$_.NetworkName -eq "Some Network"} | Set-NetworkAdapter -NetworkName "AnotherNetwork" -confirm:$true
As you can see above, it reads the VMs in the folder location, gets the current network adapter properties – in particular the networkname and then sets that networkname to the network that we want it to be. ย I placed a -confirm:$true on the end just to make sure things were setting as expected.
You can easily take the confirmation off the PowerCLI one liner and it will blast through the VMs in the blink of an eye.
Final Thoughts
There are so many PowerCLI network one liners and other scripts out there that make your life as a virtualization admin so much easier. ย Mundane tasks such as used toย change network label for all VMs in a folder can easily be done with PowerCLI.