Add a port group to a dvSwitch Vmware

dvSwitches are great unless you have to add 100 port groups to them with some pattern.  Power CLI can solve that issue.   You have to identify your dvSwitch by right clicking on the switch then you should be able to add port groups from PowerCLI using the output of this snippet of code.  This is desgined to be run from a linux machine with the output to a file which can be run in PowerCLI

#!/bin/bash
i=300
        echo "# ------ Add VLAN $i -----------"
        echo "\$spec = New-Object VMware.Vim.DVPortgroupConfigSpec[] (1)"
        echo "\$spec[0] = New-Object VMware.Vim.DVPortgroupConfigSpec"
        echo "\$spec[0].name =\"VLAN ${i}\""
        echo "\$spec[0].numPorts = 20"
        echo "\$spec[0].defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting"
        echo "\$spec[0].defaultPortConfig.vlan = New-Object VMware.Vim.VmwareDistributedVirtualSwitchVlanIdSpec"
        echo "\$spec[0].defaultPortConfig.vlan.inherited = \$false"
        echo "\$spec[0].defaultPortConfig.vlan.vlanId = $i"
        echo "\$spec[0].type = \"earlyBinding\""
        echo "\$_this = Get-View -Id 'VmwareDistributedVirtualSwitch-dvs-1246'"
        echo "\$_this.AddDVPortgroup_Task(\$spec)"
for (( i=403 ; i <= 704; i++ ))
do
        echo "# ------ Add VLAN $i -----------"
        echo "\$spec = New-Object VMware.Vim.DVPortgroupConfigSpec[] (1)"
        echo "\$spec[0] = New-Object VMware.Vim.DVPortgroupConfigSpec"
        echo "\$spec[0].name =\"VLAN ${i}\""
        echo "\$spec[0].numPorts = 8"
        echo "\$spec[0].defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting"
        echo "\$spec[0].defaultPortConfig.vlan = New-Object VMware.Vim.VmwareDistributedVirtualSwitchVlanIdSpec"
        echo "\$spec[0].defaultPortConfig.vlan.inherited = \$false"
        echo "\$spec[0].defaultPortConfig.vlan.vlanId = $i"
        echo "\$spec[0].type = \"earlyBinding\""
        echo "\$_this = Get-View -Id 'VmwareDistributedVirtualSwitch-dvs-1246'"
        echo "\$_this.AddDVPortgroup_Task(\$spec)"
done

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.