Last week I demonstrated how you can easily leverage the new PowerCLI 5.5 VDS cmdlets to migrate from a VSS (Virtual Standard Switch) to a VDS (vSphere Distributed Switch). During the development of the script, I needed a way to easily jump back and fourth between VSS->VDS and VDS->VSS and I wanted to automate this so I did not have to use the UI to reset my environment.
I initially thought this was not possible after playing around with a couple of the cmdlets but thanks to Kamen, a PowerCLI Engineer who was able to provide me with the necessary information to create a reverse migration script going from VDS to VSS.
Here is what my lab environment looks like which includes three ESXi hosts connected to a VDS called "VDS-01" which is backed by 4 pNICSs. The VDS contains 3 VMkernel interfaces and here are their respective DVPortgroup names: Management Network, Storage Network and vMotion Network.
On each ESXi host, there is an already created VSS called "vSwitch0". If one is not created or if you decide to name it something differently, then you will need to modify the script. Here is a quick screenshot of what that looks like
The PowerCLI example script below uses the Add-VirtualSwitchPhysicalNetworkAdapter cmdlet which accepts a list of pNICs, VMkernel interfaces and the portgroups to migrate from VDS to VSS. The order in which the VMkernel and portgroups are specified is critically important as they will be assigned based on the provided ordering. The script also create the necessary portgroups on the VSS which of course can be modified based on your environment. Once the migration has been completed, it will then use the Remove-VDSwitchVMHost cmdlet to remove the ESXi hosts from the VDS.
Disclaimer: Please ensure you test this script in a development/test lab before using it in a production environment.
Connect-VIServer -Server vcenter55-1.primp-industries.com -User *protected email* -Pass vmware</b></i> # ESXi hosts to migrate from VSS->VDS $vmhost_array = @("vesxi55-1.primp-industries.com","vesxi55-2.primp-industries.com","vesxi55-3.primp-industries.com") # VDS to migrate from $vds_name = "VDS-01" $vds = Get-VDSwitch -Name $vds_name # VSS to migrate to $vss_name = "vSwitch0" # Name of portgroups to create on VSS $mgmt_name = "Management Network" $storage_name = "Storage Network" $vmotion_name = "vMotion Network" foreach ($vmhost in $vmhost_array) { Write-Host "`nProcessing" $vmhost # pNICs to migrate to VSS Write-Host "Retrieving pNIC info for vmnic0,vmnic1,vmnic2,vmnic3" $vmnic0 = Get-VMHostNetworkAdapter -VMHost $vmhost -Name "vmnic0" $vmnic1 = Get-VMHostNetworkAdapter -VMHost $vmhost -Name "vmnic1" $vmnic2 = Get-VMHostNetworkAdapter -VMHost $vmhost -Name "vmnic2" $vmnic3 = Get-VMHostNetworkAdapter -VMHost $vmhost -Name "vmnic3" # Array of pNICs to migrate to VSS Write-Host "Creating pNIC array" $pnic_array = @($vmnic0,$vmnic1,$vmnic2,$vmnic3) # vSwitch to migrate to $vss = Get-VMHost -Name $vmhost | Get-VirtualSwitch -Name $vss_name # Create destination portgroups Write-Host "`Creating" $mgmt_name "portrgroup on" $vss_name $mgmt_pg = New-VirtualPortGroup -VirtualSwitch $vss -Name $mgmt_name Write-Host "`Creating" $storage_name "Network portrgroup on" $vss_name $storage_pg = New-VirtualPortGroup -VirtualSwitch $vss -Name $storage_name Write-Host "`Creating" $vmotion_name "portrgroup on" $vss_name $vmotion_pg = New-VirtualPortGroup -VirtualSwitch $vss -Name $vmotion_name # Array of portgroups to map VMkernel interfaces (order matters!) Write-Host "Creating portgroup array" $pg_array = @($mgmt_pg,$storage_pg,$vmotion_pg) # VMkernel interfaces to migrate to VSS Write-Host "`Retrieving VMkernel interface details for vmk0,vmk1,vmk2" $mgmt_vmk = Get-VMHostNetworkAdapter -VMHost $vmhost -Name "vmk0" $storage_vmk = Get-VMHostNetworkAdapter -VMHost $vmhost -Name "vmk1" $vmotion_vmk = Get-VMHostNetworkAdapter -VMHost $vmhost -Name "vmk2" # Array of VMkernel interfaces to migrate to VSS (order matters!) Write-Host "Creating VMkernel interface array" $vmk_array = @($mgmt_vmk,$storage_vmk,$vmotion_vmk) # Perform the migration Write-Host "Migrating from" $vds_name "to" $vss_name"`n" Add-VirtualSwitchPhysicalNetworkAdapter -VirtualSwitch $vss -VMHostPhysicalNic $pnic_array -VMHostVirtualNic $vmk_array -VirtualNicPortgroup $pg_array -Confirm:$false } Write-Host "`nRemoving" $vmhost_array "from" $vds_name $vds | Remove-VDSwitchVMHost -VMHost $vmhost_array -Confirm:$false <i><b>Disconnect-VIServer -Server $global:DefaultVIServers -Force -Confirm:$false
Here is a screenshot of the script executing:
Shay says
Hi,
I ran the script in my environment, and the scripts fails when migrating the port group.
The command "Add-VirtualSwitchPhysicalNetworkAdapter" fails with the error "An error occurred while communicating with the remote host".
Are you familiar with this error?
Thank you.
William Lam says
I've seen this error in other situation, but it sounds like your host may be experiencing some networking issues, either during the migration or hostd itself is having some problems.
Alex says
Hey William, I'm seeing the same issue on multiple hosts in multiple environments. I don't think it likes the switch over with the vmk management interface. If I'm thinking of this correctly, if I were to step through this manually in the Web Client, what you'd be doing is trying to add physical adapters to the vSwitch0. However, being that the physical adapters are already in use on the DVS, they will not show up as available physical network adapters to add when attempting to on the vSwitch0... Does that make sense?
Joey says
Hi
What do I need to modify to move dvs to vss and portgroup for VM network? All the VMs will need to change portgroup labels to the vss portgroups
Thanks
Edward says
Indeed that's exactly the large issue right? Moving things like vmks is one thing, but when you have VM Port groups with hundreds of vm's that use the vds how can you automate that portion?
ikiris says
Very useful article, thanks!
Trevor says
Still a great workflow for vSphere 6.0. Thanks!
Chris S. says
I cannot get this to work. The Add-VirtualSwitchPhysicalNetworkAdapter part fails when attempting to do the mgmt vmk0. Vmotion works fine.
ilford says
Hi William, How can I add the vlan ids of the port groups such that they get migrated to standard switch.
ilford says
I figured out I could add it during the creation of the portgroups
Write-Host "`Creating" $vmotion_name "portrgroup on" $vss_name
$vmotion_pg = New-VirtualPortGroup -VirtualSwitch $vss -Name $vmotion_name -VlanID 114
Ninglen says
Hi William,
I would like to do the same using python.
Can you please pint to the right method/proc for migrating the vmks using python ?
I need it urgent