Virtually everything is poshable
PowerCLI: Mass VM Portgroup Change
I was asked in my Script List if it was possible to change Multiple VMs to attach to a new PortGroup, this task is easy enough so long as you remember that every VM can have up to 4 virtual nics in VI3 and up to 10 in vSphere.
So to change all virtual machines we would do the following:
$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"
Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false
Or to change all of the VMs for a particular cluster we could use:
$Cluster = "Non-Production"
$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"
Get-Cluster $Cluster |Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false
Note: By default the Set-NetworkAdapter cmdlet would prompt you for each change it was going to make, this is overwritten by using the –Confirm:$false parameter.
VESI
This can also be achieved from the VESI by:
- Connect to a managed host
- Select Virtual Machines
- Select the virtual machines from the grid view which you would like to change
- Click ‘Network Adapters’ from the links pane on the right
- Filter the grid view to only display the adapters attached to a certain Network Name
- Click ‘Change Properties’ from the Actions pane on the right and enter a new NetworkName as seen below
| Print article | This entry was posted by Virtu-Al on October 19, 2009 at 11:45, and is filed under PowerCLI, powershell, vmware. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







(3)
(13)
(0)
about 10 months ago
Awesome! Totally awesome!! Thanks!@
about 6 months ago
What if i had to change only the name of the portgroup, for example from a to b, the minute i change it in the host level, all the vm’s will lose the network, will this script still work? or it will fail to get the old network?