Published on

VMware vSphere API

Authors
  • Name
    Jackson Chen

How to configure distributed portgroup

https://developer.vmware.com/docs/powercli/latest/vmware.vimautomation.vds/commands/set-vdportgroup/#Default

https://vexpert.co.nz/2020/03/06/create-multiple-vds-port-groups-using-powercli/

https://developer.vmware.com/docs/powercli/latest/vmware.vimautomation.vds/commands/set-vdportgroup/#Default

## Example
# Example 1
# portgroups.csv
portgroup,vlan
test-pg,1000

$PGs = Import-CSV .\portgroups.csv
$VDS = "VDS Name"
$RefPG = Get-VDPortgroup -Name "Reference Portgroup Name"
 
ForEach ($PG in $PGs) {
    $newPG = Get-VDSwitch -Name $VDS |
    New-VDPortgroup -Name $PG.portgroup  -ReferencePortgroup $RefPG.Name | Set-VDPortgroup -Notes $PG.description
    Set-VDVlanConfiguration -VDPortgroup $newPG -VlanId $PG.vlan -Confirm:$false
    }


# Example 2
$vCenter = 'vcsa01.lab.local'
$vDSName = "lab-vds"
$vdPG = "New-Portgroup"
$vLAN = "12"        # $vLAN = 0   # if no vlan tag required 
$Ports = "8"
$activeUp = ("Uplink 1", "Uplink 2")
$standUp = "Uplink 3"
$unUsedUp = "Uplink 4"


# Connect to vCenter
Connect-VIServer $vCenter -Credential (Get-Credential) -Force

# Create distributed port group
Get-VDSwitch -Name $vDSName | New-VDPortGroup -Name $vdPG -VLandId $$vLAN -NumPorts $Ports

# Set portgroup options
Get-VDSwitch -Name $vDSName | Get-VDPortGroup $vdPG | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkport $activeUp

# Verify the newly created distributed port group
Write-Host "'nPortgroups created, verify the settings"
Get-VDSwitch -Name $vDSName | Get-VDPortGroup $vdPG | Select Name, numports, portbinding, vlanconfiguration

Configure distributed portgroup NetFlow

https://subscription.packtpub.com/book/cloud-and-networking/9781785286858/4/ch04lvl1sec36/configuring-netflow

To configure Netflow, we need to access APIs. To do this, we will access the ReconfigureDVPortgroup_Task method. Alan Renouf has a very good blog and description for this (http://www.virtu-al.net/2013/07/23/disabling-netflow-with-powercli/):

For those who are trying to enable NetFlow on vDS ports the following changes are required.

$spec.defaultPortConfig.ipfixEnabled.inherited = $false
$spec.defaultPortConfig.ipfixEnabled.value = $true

List all VDPortGroups and if Netflow is enabled

Get-VDPortgroup | Select Name, VirtualSwitch, @{Name="NetflowEnabled";Expression={$_.Extensiondata.Config.defaultPortConfig.ipfixEnabled.Value}}
Function Disable-PGNetflow {
   [CmdletBinding()]
   Param (
      [Parameter(ValueFromPipeline=$true)]
      $DVPG
   )
   Process {
      Foreach ($PG in $DVPG) {
         $spec = New-Object VMware.Vim.DVPortgroupConfigSpec
         $spec.configversion = $PG.Extensiondata.Config.ConfigVersion
         $spec.defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting
         $spec.defaultPortConfig.ipfixEnabled = New-Object VMware.Vim.BoolPolicy
         $spec.defaultPortConfig.ipfixEnabled.inherited = $false
         $spec.defaultPortConfig.ipfixEnabled.value = $false
 
         $PGView = Get-View -Id $PG.Id
         $PGView.ReconfigureDVPortgroup_Task($spec)
      }
   }
}
 
# Disable Netfow for a VDPortgroup
Get-VDPortgroup DPortGroup | Disable-PGNetflow

To verify a specific portgroup netflow value

(Get-VDPortgroup -Name TestPortGroup).Extensiondata.Config.defaultPortConfig.ipfixEnabled.Value

Next, we will change the value. As seen in the following output, it takes a VMware.Vim.DVPortgroupConfigSpec type specification:

PS C:\> (Get-VDPortgroup -Name "TestPortGroup").Extensiondata.ReconfigureDVPortgroup_Task

To do this, we will first create a specification and then pass the value to the method in order to make the change:

$dvPG = Get-VDPortgroup -Name "TestPortGroup"
$pSpec = New-Object VMware.Vim.DVPortgroupConfigSpec
$pSpec.configversion = $dvPG.Extensiondata.Config.ConfigVersion
$pSpec.defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting
$pSpec.defaultPortConfig.ipfixEnabled = New-Object VMware.Vim.BoolPolicy
$pSpec.defaultPortConfig.ipfixEnabled.inherited = $false
$pSpec.defaultPortConfig.ipfixEnabled.value = $true

(Get-VDPortgroup -Name "TestPortGroup" | Get-View).ReconfigureDVPortgroup_Task($pSpec)


# To disable the netflow
$pSpec.defaultPortConfig.ipfixEnabled.value = $false