PowerCLI script to Copy a DVS to a VSS

As part of the ESXi vCenter Migration that I performed recently, we had to convert the ESXi hosts back to Standard vSwitches (from the Distributed vSwitch).  The built-in Wizards make the transition from Standard to Distributed nice and easy, but I don't know of any such path for easily moving the other direction.  We determined that we'd manually create the Standard vSwitch with all of the port groups from the Distributed vSwitch... but there were about 20 port groups that needed to be transitioned for each of the hosts in the cluster.  Aside from the arduous labor involved in that process, I was concerned about typos that might be introduced by depending on manual labor to accomplish that process.  So, I wrote a quick PowerCLI script to take care of it.

This script takes 3 arguments; Host, Source and Destination.  The Host is the ESXi Host on which the operation should be performed.  The Source is the Distributed vSwitch that should be copied (this code is specifically written for a Distributed vSwitch source).  The Destination is the (already existing) Standard vSwitch onto which the port groups should be recreated.  This script will create VSS port groups with the same names and VLAN tags as their DVS counterparts (which makes it nice and easy to move the VMs onto the VSS port groups).

As always, this is a script that I wrote for a specific use case.  While I used it successfully in this environment, there are no guarantees about how it will work in your environment, so please feel free to edit (fix) the script as needed!

#Copies all Port Groups from a Distributed vSwitch to a Standard vSwitch
#Author: Jason Coleman (virtuallyjason.blogspot.com)
#Usage: Make-VSS.ps1 -h [the target ESXi host to get the new standard switch, must have access to the DVS] -s [the name of the DVS to copy] -d [the name of the VSS to copy the Port Groups to]


param(
[alias("h")]
[string]$thisHost = $(read-host -Prompt "Enter the target Host"),
[alias("s")]
[string]$source = $(read-host -Prompt "Enter the Source Distributed Virtual Switch name"),
[alias("d")]
[string]$destination = $(read-host -Prompt "Enter the Destination Virtual Switch name")
)

#Get the destination vSwitch
$destSwitch = Get-VirtualSwitch -host $thisHost -name $destination
#Get a list of all port groups on the source distributed vSwitch
$allPGs = get-vmhost $thisHost | get-vdswitch -name $source | get-vdportgroup
foreach ($thisPG in $allPGs)
{
new-virtualportgroup -virtualswitch $destSwitch -name $thisPG.Name
#Ensure that we don't try to tag an untagged VLAN
if ($thisPG.vlanconfiguration.vlanid)
{
get-virtualportgroup -virtualswitch $destSwitch -name $thisPG.Name | Set-VirtualPortGroup -vlanid $thisPG.vlanconfiguration.vlanid
}
}

Comments

Post a Comment

Sorry guys, I've been getting a lot of spam recently, so I've had to turn on comment moderation. I'll do my best to moderate them swiftly after they're submitted,

Popular posts from this blog

Clone a Standard vSwitch from one ESXi Host to Another

PowerShell Sorting by Multiple Columns

Deleting Orphaned (AKA Zombie) VMDK Files