WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud Foundation
  • VKS
  • Homelab
    • Resources
    • Nested Virtualization
  • VMware Nostalgia
  • Apple
You are here: Home / Automation / Automate the reverse, migrating from vSphere Distributed Switch to Virtual Standard Switch using PowerCLI 5.5

Automate the reverse, migrating from vSphere Distributed Switch to Virtual Standard Switch using PowerCLI 5.5

11.05.2013 by William Lam // 14 Comments

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-&gt;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:

More from my site

  • Automate the migration from Virtual Standard Switch to vSphere Distributed Switch using PowerCLI 5.5
  • Quick Tip - Retrieving vSphere Distributed Switch (VDS) DVPort ID & Stats using PowerCLI
  • Moving ESXi hosts with LACP/LAG between vCenter Servers?
  • Retrieving statistics for a Distributed Virtual Port using the vSphere API & PowerCLI
  • How to manually clean up a Distributed Virtual Switch (VDS) on an ESXi host?

Categories // Automation, PowerCLI Tags // distributed virtual switch, migration, PowerCLI, vds, vSphere 5.5, vss

Comments

  1. *protectedShay says

    09/23/2014 at 12:41 pm

    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.

    Reply
    • William Lam says

      09/23/2014 at 2:10 pm

      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.

      Reply
      • *protectedAlex says

        11/22/2015 at 10:34 am

        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?

        Reply
  2. *protectedJoey says

    10/21/2014 at 3:02 am

    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

    Reply
    • *protectedEdward says

      06/19/2018 at 5:23 pm

      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?

      Reply
  3. *protectedikiris says

    05/14/2015 at 6:17 pm

    Very useful article, thanks!

    Reply
  4. *protectedTrevor says

    07/29/2015 at 12:33 am

    Still a great workflow for vSphere 6.0. Thanks!

    Reply
    • *protectedChris S. says

      08/04/2015 at 11:06 pm

      I cannot get this to work. The Add-VirtualSwitchPhysicalNetworkAdapter part fails when attempting to do the mgmt vmk0. Vmotion works fine.

      Reply
  5. *protectedilford says

    10/18/2015 at 4:39 am

    Hi William, How can I add the vlan ids of the port groups such that they get migrated to standard switch.

    Reply
    • *protectedilford says

      10/18/2015 at 5:56 am

      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

      Reply
  6. *protectedNinglen says

    01/14/2016 at 1:41 am

    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

    Reply

Trackbacks

  1. Retrieving statistics for a Distributed Virtual Port using the vSphere API & PowerCLI | virtuallyGhetto says:
    10/25/2016 at 8:10 am

    […] Standard Switch (VSS) to Distributed Virtual Switch (VDS) and from a VDS back to VSS (here & here), I have not done a whole lot with the vSphere API with regards to individual Distributed Virtual […]

    Reply
  2. Automate the migration from Virtual Standard Switch to vSphere Distributed Switch using PowerCLI 5.5 | virtuallyGhetto says:
    10/25/2016 at 8:16 am

    […] is a PowerCLI cmdlet that can be used to migrate from VDS->VSS. I will be sharing that script in another blog post for those that may want to perform the […]

    Reply
  3. Migrating ESXi Hosts with a vDS between vCenter servers using Powershell | vKARPS says:
    01/24/2018 at 3:32 pm

    […] as you do when moving the other direction (vSS to vDS), but I did come across this post from @Lamw here which cuts them all over along with the adapter. I have modified it slightly for this scenario. […]

    Reply

Thanks for the comment!Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search

Thank Author

Author

William is Distinguished Platform Engineering Architect in the VMware Cloud Foundation (VCF) Division at Broadcom. His primary focus is helping customers and partners build, run and operate a modern Private Cloud using the VMware Cloud Foundation (VCF) platform.

Connect

  • Bluesky
  • Email
  • GitHub
  • LinkedIn
  • Mastodon
  • Reddit
  • RSS
  • Twitter
  • Vimeo

Recent

  • Programmatically accessing the Broadcom Compatibility Guide (BCG) 05/06/2025
  • Quick Tip - Validating Broadcom Download Token  05/01/2025
  • Supported chipsets for the USB Network Native Driver for ESXi Fling 04/23/2025
  • vCenter Identity Federation with Authelia 04/16/2025
  • vCenter Server Identity Federation with Kanidm 04/10/2025

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2025