PowerCLI Script to create new vDS Portgroups

We deployed new hosts into one of our office locations.  Because these hosts would have different number of uplinks as well as use a LAG group instead of normal dvUplinks, we decided to create a new vDS Switch.  Then, we needed to re-create all the portgroups as well.  I figured, instead of manually adding 15-20 portgroups, I’ll automate it.  I used this blog post as reference to do some of the work, and added the LAG and uplink changes.

When you create a new portgroup, it automatically includes the dvUplinks, but in this case, we wanted to remove them and use the LAG group.  We also manually created the switch and the LAG group before running this switch.

The script accepts the input of a CSV which has 3 columns, pgName, numports, and vlanID.  From there, I was able to do what I needed.  The last step the script lists the Portgroups for validation.


###########################################
#
# Script to create vDS Portgroups
# Created by BLiebowitz on 5/31/2017
#
###########################################

# Connect to vCenter
# Connect-viserver vCenter

# Set the VDS Name to variable
$vds = "dvSwitch"
$vdsname = get-vdswitch $vds

# Import the CSV of VLAN IDs, Portgroups, and # of ports
$vdsPortgroup = Import-Csv e:\ben\vmware\New_Portgroups.csv

# Loop through each VLAN and create it in the vDS
foreach ($portgroup in $vdsPortgroup){
Get-vdswitch $vdsname | New-VDPortgroup -name $portgroup.pgName -NumPorts $portgroup.numports -VlanId $portgroup.vlanID
get-vdswitch $vdsname | Get-VDPortgroup $portgroup.pgName | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -UnusedUplinkPort dvUplink1, dvUplink2, dvUplink3, dvUplink4
get-vdswitch $vdsname | Get-VDPortgroup $portgroup.pgName | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort LAG
}

# List the vLANS after creation
Write-Host "`nPortgroups created. Now confirming settings" -ForegroundColor Cyan
Get-VDSwitch $vdsname | Get-VDPortgroup | select name, numports, portbinding, vlanconfiguration

I hope someone finds this useful

Ben Liebowitz, VCP, vExpert
NJ VMUG Leader

 

Share This:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.