vSphere: Migrating vSwitch PortGroups and VLANS

KB ID 0001578

Problem

Over the past few weeks I’ve been doing some work for a client that involves me adding some new ESX hosts. These will be setup the same as their existing ESX hosts. That’s fine, but they use standard vSwitches and have A LOT of port groups and VLANs.

I could sit and create them all manually, and tag them onto the the correct VLANs, but something that’s boring and repetitive is better solved with a bit of scripting!

Pre Requisites: You will need VMware PowerCLI installing (Install-Module -Name VMware.PowerCLI)

Solution

Connect to your vCenter

Connect-VIServer server-name.domain-name.com

Note: If it complains about certificates (see below) issue this command ‘Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false‘ then try to connect again

Connect-VIServer Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Prompt if you’d like to connect once or to add a permanent exception for this server.

Note2: If migrating between hosts on different vCenters, then Connect-VIServer to BOTH vCenters before proceeding.

Once connected use the following two commands to specify your existing SOURCE ESX server name, and your new TARGET ESX server names, change yours accordingly.

$sourcehost = get-vmhost -name existing-esx.domain-name.com
$targethost = get-vmhost -name new-esx.domain-name.com

Now, to copy and create all the port groups and VLANs use the following command. Note: Change the vSwitch numbers accordingly to match the source and destination vSwitch you are copying from and to.

$sourcehost | get-virtualswitch -name vSwitch0 | get-virtualportgroup | foreach { $newportgroup = $targethost | get-virtualswitch -name vSwitch0 | new-virtualportgroup -name $_.Name -VLanID $_.VLanID }

You can observe them getting created in the vSphere web client like so;

VMware Migrate Port Groups and VLANS Standard

Note: If you are migrating ‘vanilla’ Port Groups, you may see a couple of errors fly past if it tries to create ‘Management Network‘ and ‘VM Network‘ as these exist by default, so it can’t recreate them.

Related Articles, References, Credits, or External Links

NA

Author: PeteLong

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *