Duplicate Port Groups using PowerCLI
Recently I needed to duplicate the configuration of one Distributed Switch (vDS) to another blank vDS that I created. I wrote a PowerCLI script to do this automagically. I thought I would share it in case anyone else needed to do the same. Here it is…
Script Overview
The vDS I was duplicating had around 25 port groups, each with their own configuration. To eliminate human error and the sheer boredom of manually recreating all the port groups, I wrote a PowerCLI script.
Before getting into the script itself let us review the requirements:
1. Duplicate port groups and all of their configuration from the original vDS into a new vDS (which has already been created)
2. Be able to select which port groups to duplicate (in the event that you don’t want to do them all)
3. Be able to duplicate port groups from different distributed switches into a new vDS (many to one)
4. Select the new name for the port group (this is a requirement as vCenter does not allow for port groups with the same name, even if they are in a different vDS)
5. Be able to log and review the results
Pre-Reqs
This script has the following requirements that need to be adhered to:
1. There needs to be an Input.csv file in the root directory of the script (see section “How does the script work?” below for more information)
2. You need PowerCLI installed on the machine you will be running this on
3. You require at least PowerShell v3 installed
4. You need my PowerShell Logging Functions installed and dot sourced to the directory you have them stored in (line 35). If you do not yet have the PowerShell Logging Function or you don’t know what they are then you can find all of the information here >>> PowerShell: How to easily create log files for your scripts
5. The new distributed switch already created in vCenter (This script does not create a new vDS, although it is very easy to modify it to do so if required)
How does the script work?
In order to be able to select the individual port groups you want duplicated and specify their new name, the script requires a Input.csv file to be present in the root of the script directory.
This file is comma delimited with simply the name of the port group you want to copy and then the name of the new port group. The only other requirements is a header of orig and new. Below is an Input.csv example:
orig,new
VLAN_1,vPG_VLAN_01
VLAN_2,vPG_VLAN_02
VLAN_3,vPG_VLAN_03
VLAN_10,vPG_VLAN_10
Note: The new port group name must be unique and not the same as the original port group name.
The script will prompt the user for the name of the “new” distributed switch that you want to copy the port groups into. This distributed switch needs to be pre-created and already exist as this script does not create a switch for you (although it wouldn’t be hard to modify the script to do so if required).
The script then enumerates the Input.csv file, searching and storing the “orig” port group as an object and then creating a new port group using the “orig” port group object’s configuration.
The Script: Duplicate Port Groups using PowerCLI
Here is the script in its entirety. After this we will go through the main parts of the script:
Script Details
For those of you who are interested, I thought I might put some comments around the core parts of the script and how it completes the major steps:
1. Reading the CSV file
Reading data in PowerShell is very easy, and for CSV files its even easier – just use the Import-Csv
cmdlet and store the results in a variable:
#Collect info from CSV file
$FilePath = Join-Path -Path $PSScriptRoot -ChildPath Input.csv
$Global:Data = Import-Csv -Path $FilePath
2. Prompt the user for data
Using the Read-Host
cmdlet we can prompt the user for the data we require. In this script we are prompting the user twice – once for the name or IP of the vCenter server (line 146) and the second time for the name of the distributed switch that will contain the duplicated port groups. This data is stored in variables and then used throughout the script when required.
$Server = Read-Host "Specify the vCenter Server to connect to (IP or FQDN)?"
$Global:dvNewName = Read-Host "What is the name of the new vDS?"
3. Create a new port group using the settings of an existing port group
Step 1 is to collect and store the existing port group in a variable. This is done by this line:
$PG = Get-VDPortgroup -Name $Row.orig
Note: The $Row.orig
refers to the first comma delimited entry within the CSV file (i.e. the original port group name). We use .orig
to collect the value as the header (first line) of the CSV file is set to orig,new.
If we changed it to let’s say old,new then we would be using $Row.old
instead. The same logic applies to $Row.new
in step 2 below.
Step 2 is to create a new port group in the vDS specified, using the existing port group’s configuration as a reference. This is done by using the -ReferencePortGroup
parameter on the New-VDPortGroup
cmdlet.
Get-VDSwitch -Name $Global:dvNewName | New-VDPortgroup -Name $Row.new -ReferencePortgroup $PG
And that is pretty much it. If you don’t want to use my entire script, then you can achieve a similar result by using these two lines and simply specifying the switch name and the port groups name:
$PG = Get-VDPortgroup -Name "orig_port_group_name"
Get-VDSwitch -Name "switch_name" | New-VDPortgroup -Name "new_port_group_name" -ReferencePortgroup $PG
If you have any questions, comments or suggestions let me know below….
Enjoy!
Luca