Powershell script to create vsphere distributed virtual switch port groups

I used the following Powershell Powercli script to create a bunch of DVS Port Groups on a VSAN vSphere 6 cluster.  Using the command line speeds things up and eliminates minimizes the impact of human error – no more mouse clicking.
Based on the work from the following blog post – I adapted it for our environment:
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.
 
 
————————-
Here is my script… it created 41 port groups with the name “VM Network 3500” and so on…
Iben Rodriguez – 2015
$vDS = “SNSJ-DSwitch”
$Reference_DPG = “DPortGroup”
$DPGs = @{
     “3500”  = “VM Network”;
     “3501”  = “VM Network”;
     “3502”  = “VM Network”;
     “3503”  = “VM Network”;
     “3504”  = “VM Network”;
     “3505”  = “VM Network”;
     “3506”  = “VM Network”;
     “3507”  = “VM Network”;
     “3508”  = “VM Network”;
     “3509”  = “VM Network”;
     “3510”  = “VM Network”;
     “3511”  = “VM Network”;
     “3512”  = “VM Network”;
     “3513”  = “VM Network”;
     “3514”  = “VM Network”;
     “3515”  = “VM Network”;
     “3516”  = “VM Network”;
     “3517”  = “VM Network”;
     “3518”  = “VM Network”;
     “3519”  = “VM Network”;
     “3520”  = “VM Network”;
     “3521”  = “VM Network”;
     “3522”  = “VM Network”;
     “3523”  = “VM Network”;
     “3524”  = “VM Network”;
     “3525”  = “VM Network”;
     “3526”  = “VM Network”;
     “3527”  = “VM Network”;
     “3528”  = “VM Network”;
     “3529”  = “VM Network”;
     “3530”  = “VM Network”;
     “3531”  = “VM Network”;
     “3532”  = “VM Network”;
     “3533”  = “VM Network”;
     “3534”  = “VM Network”;
     “3535”  = “VM Network”;
     “3536”  = “VM Network”;
     “3537”  = “VM Network”;
     “3538”  = “VM Network”;
     “3539”  = “VM Network”;
     “3540”  = “VM Network”;     
};
$DPGs.Keys | % {
     $VLAN = $_
     $DPG = $DPGs.Item($_) + ” ” + $VLAN
     New-VDPortgroup -VDSwitch $vDS -Name $DPG -ReferencePortgroup $Reference_DPG
     Set-VDPortgroup -VDPortgroup $DPG -VlanId $VLAN
}
 
Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s