I made a simple script for 1 job : changing the portgroup for multiple vm’s in the same portgroup.
The script will ask which portgroup you want to move vm’s from using a selection after that using the same method it will ask you for the portgroup to move to. Then it will change all the vm’s using that portgroup to the new one. Pretty simple but if you are a little bit smart about it you can change this to do even more and maybe make it a bit prettier because now you need to put in the vcenter name manually in the script.
I made 2 versions of this, one for standard switch and one for Distributed switch which i will both add below.
Standard Switch Script
Connect-VIServer VCENTERNAME
$pg = Get-VirtualPortGroup | Out-GridView -OutputMode Single -Title "Select Current Portgroup"
$pgName = $pg.Name
$pg = Get-View -ViewType Network -Property Name,VM -Filter @{Name=$pgName}
$vmlist = Get-View -Id $pg.Vm -Property Name | Select-Object -ExpandProperty Name
####################################
$newpg = Get-VirtualPortGroup | Out-GridView -OutputMode Single -Title "Select New Portgroup"
$newpgname = $newpg.Name
foreach ($vm in $vmlist){
Get-VM -Name $vm | Get-NetworkAdapter | where{$_.NetworkName -eq $pg.Name} | Set-NetworkAdapter -NetworkName $newpg.Name -Confirm:$false
}
Distributed Switch Script
Connect-VIServer VCENTERNAME
$pg = Get-VDPortGroup | Out-GridView -OutputMode Single -Title "Select Current Portgroup"
$pgName = $pg.Name
$pg = Get-View -ViewType Network -Property Name,VM -Filter @{Name=$pgName}
$vmlist = Get-View -Id $pg.Vm -Property Name | Select-Object -ExpandProperty Name
####################################
$newpg = Get-VDPortGroup | Out-GridView -OutputMode Single -Title "Select New Portgroup"
$newpgname = $newpg.Name
foreach ($vm in $vmlist){
Get-VM -Name $vm | Get-NetworkAdapter | where{$_.NetworkName -eq $pg.Name} | Set-NetworkAdapter -NetworkName $newpg.Name -Confirm:$false
}
The original article was posted on: www.hollebollevsan.nl