Mass Add Portgroups to Distributed Virtual Switch with .csv

Post date: Nov 12, 2013 4:50:30 PM

For new deployments you may have hundreds of Portgroups to ad to a VDS. This allows to mass deploy and configure some additional security from a .csv list. This assumes you have only 1 VDS in you environment otherwise modify $VDSwitch to select the correct VDS you wish to add PGs too. You could also add another column for vdswitch to include multiple VDSwitches.

There is a limit to the number of ports a single VDS can have. To eliminate over port provisioning, count how many interfaces will be needed per portgroup for the portcount column.

*** Ok there is an issue with this that I am now aware of. Setting the dvUplink to reject reject reject WILL prevent a VM's traffic... as the uplink normally says "uplink" I'm currently using Get-VDPortgroup | ?{$_.Name -notlike "*uplink*"} to work-around this.

##################################################################

#Author: KnightUSN

#MassAddVDS-PG.ps1 v1 - 11/8/2013

#

#Create a .CSV with the following Columns

#NAME VLAN PORTCOUNT

#

#Useage: MassAddVDS-PG.ps1 ./PortGroupList.csv

##################################################################

param(

[parameter(Mandatory = $true)]

[string[]]$filename

)

#Getting List of PGs from .csv

$PGs = Import-CSV $filename -UseCulture

$VDSwitch = Get-VMhost | Get-VDSwitch

#Add VPGs to VDS

ForEach($PG in $PGs){$VDSwitch | New-VDPortgroup -Name $PG.Name -NumPorts $PG.Portcount -VlanId $PG.VLAN}

#Set REJECT REJECT REJECT

#$VDSwitch | Get-VDPortgroup | Get-VDSecurityPolicy | Set-VDSecurityPolicy -MacChanges $False -ForgedTransmits $False