PowerCLI to migrate VMs from standard switch to VDS

14 Feb

We are currently upgrading ESXi 4.1 servers to ESXi 5x. One of the main change from 4 to 5 is, of course, from standard virtual switch to distributed virtual switch. With hundreds of VMs to migrate it can be a pain to do it using the vCenter network migration wizard. Also it can be a risk of too much manual changes where we may mistaken choose to migrate to a wrong VLAN and causing the live VMs to loose connectivity.

Anyway, found this on vmware documentation and clobbered a script to help myself.

This is just a sample script, you can always wrap it around a CSV file and loop through all the portgroups you want to migrate.

$esx = "hostname"
$vsname = "old_vswitch"  
$vdsname = "new_vdswitch"  
$oldpg = "old_portgroup"
$newpg = "new portgroup"

# get the 
$vdPortGroup = Get-VDPortGroup -VDSwitch (
Get-VDSwitch -Name $vdsname) -Name $newpg ;

#get all VMs in this host and migrate the network to VDS
Get-vm -Location $esx | Get-NetworkAdapter |
 where { $_.NetworkName -eq $oldpg } | 
  Set-NetworkAdapter -PortGroup $vdPortGroup -RunAsync ;

An alternative is to use related objects on the portgroups as per the vm docs for that you need to get the old portgroup object first. This is a good alternative is you are doing migration of all the VMs on a particular switch. The script above only grabs VMs hosted by an ESX host on a particular switch.

$vmsPortGroup = Get-VirtualSwitch -Name $vsname | 
Get-VirtualPortGroup -Name $oldpg

Get-vm -RelatedObject $vmsPortGroup  | Get-NetworkAdapter |
 where { $_.NetworkName -eq $vmsPortGroup.Name } | 
  Set-NetworkAdapter -PortGroup $vdPortGroup -RunAsync ;
Leave a comment

Posted by on February 14, 2014 in powershell, Scripts, vmware


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: