Creating consistent Distributed Port Groups with PowerCLI

I recently had to create a new vDS to replicate a standard vSwitch from another vCenter install. I wanted to create my vDS Distribute Port Groups (DPG) simply, but consistently. As I have a low number of DPGs to create, I could probably have done this manually, but scripting the creation ensures consistency. Plus, it’s a subset of PowerCLI that I wanted to familiarize myself with.

First, I created a vDS and a reference DPG through the vSphere Web Client. You can do this with PowerCLI, but you have to go down the rabbit hole of Views to touch some of the advanced settings, something that’s not well documented and would have been very time consuming for me to explore. I also didn’t mind creating the initial vDS and DPG as the visual view of the Web Client made it easy for me to verify the settings whereas a long string of PowerShell (PoSH) would have been a little more difficult to interpret.

My vDS is called DVS_Lab. I name my DPGs with the pattern “DPG_<VLAN>_<NAME>” (modify the script to match your pattern). This results in a reference DPG called DPG_2_Switch_Management_10.5.0.0-24. I modified the DPG slightly to set Forged Transmits and MAC changes to accept and Promiscuous to disabled. We’ll store these two names in variables:

$vDS = "DVS_Lab"
$Reference_DPG = "DPG_2_Switch_Management_10.5.0.0-24"

The next, and most difficult step, is to create a hash of the remaining DPGs. Our key:value pairing is VLAN IDs:description. You create a hash in PoSH with the following syntax:

$DPGs = @{
     "5"  = "Developer_VMNet_10.0.1.0-24";
     "10" = "Testing_VMNet_172.16.1.0-24";
     "15" = "Production_VMNet_192.168.1.0-24";
     "20" = "iSCSI_A_172.17.1.0-24";
     "20" = "iSCSIS_B_172.17.1.0-24";
     "30" = "DMZ_192.168.2.0-26";
     "40" = "OOB_Network_172.16.255.0-24"
 };

Create a hash $DPGs of your own. The last step is to iterate through the hash, creating a new DPG modeled after our reference DPG and setting the VlanID.

$DPGs.Keys | % {
     $VLAN = $_
     $DPG = "DPG_" + $VLAN + "_" + $DPGs.Item($_)
     New-VDPortgroup -VDSwitch $vDS -Name $DPG -ReferencePortgroup $Reference_DPG
     Set-VDPortgroup -VDPortgroup $DPG -VlanId $VLAN
 }

Put these three sections of PoSH code together and you have a small script that can create a large number of consistent DPGs on your vDS. Remember, automation ensures consistency, not correctness – always verify that your starting point is correct!

It may seem like a very short process to automate, especially with so few DPGs, one that might not recover the time spent automating it. However, it paid for itself later in the day when I realized I created a vDS set to v5.5 compatibility and wanted to add a v5.1 host to it! I simply removed it from the hosts, deleted the vDS, recreated it at v5.1 compatibility level, created the reference DPG, and then ran the script again.

Here’s the script in its entirety:

$vDS = "DVS_Lab"
$Reference_DPG = "DPG_2_Switch_Management_10.5.0.0-24"
$DPGs = @{
     "5"  = "Developer_VMNet_10.0.1.0-24";
     "10" = "Testing_VMNet_172.16.1.0-24";
     "15" = "Production_VMNet_192.168.1.0-24";
     "20" = "iSCSI_A_172.17.1.0-24";
     "20" = "iSCSIS_B_172.17.1.0-24";
     "30" = "DMZ_192.168.2.0-26";
     "40" = "OOB_Network_172.16.255.0-24"
};
$DPGs.Keys | % {
     $VLAN = $_
     $DPG = "DPG_" + $VLAN + "_" + $DPGs.Item($_)
     New-VDPortgroup -VDSwitch $vDS -Name $DPG -ReferencePortgroup $Reference_DPG
     Set-VDPortgroup -VDPortgroup $DPG -VlanId $VLAN
}

PowerCLI 6

In PowerCLI 6, Set-VDPortgroup‘s VlanID parameter is marked obsolete. The correct cmdlet is Set-VDVlanConfiguration. Here is what the script would look like for PowerCLI version 6.

$vDS = "DVS_Lab"
$Reference_DPG = "DPG_2_Switch_Management_10.5.0.0-24"
$DPGs = @{
     "5"  = "Developer_VMNet_10.0.1.0-24";
     "10" = "Testing_VMNet_172.16.1.0-24";
     "15" = "Production_VMNet_192.168.1.0-24";
     "20" = "iSCSI_A_172.17.1.0-24";
     "20" = "iSCSIS_B_172.17.1.0-24";
     "30" = "DMZ_192.168.2.0-26";
     "40" = "OOB_Network_172.16.255.0-24"
};
$DPGs.Keys | % {
     $VLAN = $_
     $DPG = "DPG_" + $VLAN + "_" + $DPGs.Item($_)
     New-VDPortgroup -VDSwitch $vDS -Name $DPG -ReferencePortgroup $Reference_DPG
     Set-VDVlanConfiguration -VDPortgroup  $DPG -VlanId $VLAN
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s