VMware Cloud Community
justinsmith
Enthusiast
Enthusiast

Change Port Group On VM's

Im using this script to change the Port Group on all VM's on a single host, Im looking for a script that will change all the PG's on all VM's under a specific folder, rather than the whole entire host.

# Change the port group for all VM's on a host

$vmhost = "VMHOST NAME"
$OldNetwork = "VMNetwork"
$NewNetwork = "VLAN-1091"

Get-VMHost $vmhost |Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

0 Kudos
3 Replies
LucD
Leadership
Leadership

If you replace the Get-VMHost with a Get-Folder you should be there.

Get-Folder -Name MyFolder | Get-VM | ....

Or do you mean something different ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
justinsmith
Enthusiast
Enthusiast

I couldnt thing of the Get-Folder cmdlet... that should work.

In my script I have it using the VMNetwork PG, but its actually Invalid Backing (since it came from a vDS that doesnt exist in the new cluster). Is there a way to set the PG regardless of the OLD Network name?

0 Kudos
justinsmith
Enthusiast
Enthusiast

I actually just removed the  $OldNetwork and it worked fine.

0 Kudos