Get-DistributedSwitchPortGroup Name and Datacenter problems
Earlier this year i have found two errors within the Get-DistributedSwitchPortGroup cmdlet from the book “The VMware vSphere PowerCLI Reference: Automating vSphere Administration”. The book is a great resource for automating vSphere environments and the scripts extend PowerCLI with the missing dvSwitch cmdlets and other useful stuff. Thanks for the work!
Table of Contents
- Query portgroups on multiple dvSwitches
- Query portgroups with similar names
If you have configured more than one datacenter within a vCenter and you also have configured the portgroups with the same name on dvSwitches the cmdlet will return both portgroups. The reason is that the cmdlet has no option to filter by dvSwitch.
original code
Function Get-DistributedSwitchPortGroup
{
<#
.SYNOPSIS
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.DESCRIPTION
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.PARAMETER Name
Name of the DVPG to retrieve supports wildcards.
.PARAMETER DistributedSwitch
Name of vDS to retrive the DVPG for.
.EXAMPLE
Get-DistributedSwitchPortGroup -Name PG02
.EXAMPLE
Get-DistributedSwitchPortGroup -DistributedSwitch vDS01
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true
, ValueFromPipeline=$true)]
[String]
$NAME
, [Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true)]
[String]
$DistributedSwitch
)
Begin
{
$extraparams=@{}
$extraparams["Property"] = @(
'Name'
, 'Config.Description'
, 'Config.Type'
, 'Config.DefaultPortConfig'
, 'Config.DistributedVirtualSwitch'
, 'VM'
, 'PortKeys'
, 'AlarmActionsEnabled'
)
IF ($DistributedSwitch)
{
$vDSMoRef = Get-view -Property 'moref' `
-ViewType "VmwareDistributedVirtualSwitch" `
-filter @{'Name'=$DistributedSwitch}`
-verbose:$false |
Select-Object -ExpandProperty MoRef
Select-Object -ExpandProperty Value
If ($Name)
{
$extraparams["filter"] = @{
'Name'=$Name
'Config.DistributedVirtualSwitch'="VmwareDistributedVirtualSwitch-$($vDSMoRef)"
}
}
Else
{
$extraparams["filter"] = @{
'Config.DistributedVirtualSwitch'="VmwareDistributedVirtualSwitch-$($vDSMoRef)"
}
}
}
If ($Name)
{
$extraparams["filter"] = @{'Name'=$Name}
}
}
Process
{
get-view -ViewType "DistributedVirtualPortgroup" -verbose:$false @extraparams |
Select-Object @{
Name='Name'
Expression={$_.Name}
},
@{
Name='Description'
Expression={$_.Config.Description}
},
@{
Name='PortBinding'
Expression={$_.Config.Type}
},
@{
Name='VLANID'
Expression={(($_.Config.DefaultPortConfig.Vlan.VlanId|%{
if ($_ -match "\d+") {$_}
elseIf ($_.Start -eq $_.End) {$_.Start}
Else {"{0}-{1}" -f $_.Start,$_.End}}) -join ",")}
},
@{
Name='NumbOfVMs'
Expression={$_.Vm.count}
},@{
Name='NumofPorts'
Expression={$_.PortKeys.count}
},
@{
Name='AlarmActions'
Expression={$_.AlarmActionsEnabled}
},
@{
Name='DistributedSwitch'
Expression={ Get-View $_.Config.DistributedVirtualSwitch `
-Property Name -verbose:$false |
Select-Object -ExpandProperty Name}
},
@{
Name='MoRef'
Expression={ $_.MoRef}
}
}
}
modified code
Function Get-DistributedSwitchPortGroup
{
<#
.SYNOPSIS
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.DESCRIPTION
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.PARAMETER Name
Name of the DVPG to retrieve supports wildcards.
.PARAMETER DistributedSwitch
Name of vDS to retrive the DVPG for.
.EXAMPLE
Get-DistributedSwitchPortGroup -Name PG02
.EXAMPLE
Get-DistributedSwitchPortGroup -DistributedSwitch vDS01
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true
, ValueFromPipeline=$true)]
[String]
$NAME
, [Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true)]
[String]
$DistributedSwitch
)
Begin
{
$extraparams=@{}
$extraparams["Property"] = @(
'Name'
, 'Config.Description'
, 'Config.Type'
, 'Config.DefaultPortConfig'
, 'Config.DistributedVirtualSwitch'
, 'VM'
, 'PortKeys'
, 'AlarmActionsEnabled'
)
IF ($DistributedSwitch)
{
$vDSMoRef = Get-view -Property Name `
-ViewType "VmwareDistributedVirtualSwitch" `
-filter @{'Name'=$DistributedSwitch}`
-verbose:$false |
Select-Object -ExpandProperty MoRef|Select-Object -ExpandProperty Value
If ($Name)
{
$extraparams["filter"] = @{
'Name'=$Name
'Config.DistributedVirtualSwitch'="$($vDSMoRef)"
}
}
Else
{
$extraparams["filter"] = @{
'Config.DistributedVirtualSwitch'="$($vDSMoRef)"
}
}
}
If ($Name)
{
$extraparams["filter"] = @{'Name'=$Name}
}
}
Process
{
get-view -ViewType "DistributedVirtualPortgroup" -verbose:$false @extraparams |
Select-Object @{
Name='Name'
Expression={$_.Name}
},
@{
Name='Description'
Expression={$_.Config.Description}
},
@{
Name='PortBinding'
Expression={$_.Config.Type}
},
@{
Name='VLANID'
Expression={(($_.Config.DefaultPortConfig.Vlan.VlanId|%{
if ($_ -match "\d+") {$_}
elseIf ($_.Start -eq $_.End) {$_.Start}
Else {"{0}-{1}" -f $_.Start,$_.End}}) -join ",")}
},
@{
Name='NumbOfVMs'
Expression={$_.Vm.count}
},@{
Name='NumofPorts'
Expression={$_.PortKeys.count}
},
@{
Name='AlarmActions'
Expression={$_.AlarmActionsEnabled}
},
@{
Name='DistributedSwitch'
Expression={ Get-View $_.Config.DistributedVirtualSwitch `
-Property Name -verbose:$false |
Select-Object -ExpandProperty Name}
},
@{
Name='MoRef'
Expression={ $_.MoRef}
}
}
}
If you search for a portgroup like “VMOTION” the function also finds “VMOTION-DVUplinks-48″. With the patch the function only finds the exact name.
original code
Function Get-DistributedSwitchPortGroup
{
<#
.SYNOPSIS
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.DESCRIPTION
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.PARAMETER Name
Name of the DVPG to retrieve supports wildcards.
.PARAMETER DistributedSwitch
Name of vDS to retrive the DVPG for.
.EXAMPLE
Get-DistributedSwitchPortGroup -Name PG02
.EXAMPLE
Get-DistributedSwitchPortGroup -DistributedSwitch vDS01
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true
, ValueFromPipeline=$true)]
[String]
$NAME
, [Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true)]
[String]
$DistributedSwitch
)
Begin
{
$extraparams=@{}
$extraparams["Property"] = @(
'Name'
, 'Config.Description'
, 'Config.Type'
, 'Config.DefaultPortConfig'
, 'Config.DistributedVirtualSwitch'
, 'VM'
, 'PortKeys'
, 'AlarmActionsEnabled'
)
IF ($DistributedSwitch)
{
$vDSMoRef = Get-view -Property Name `
-ViewType "VmwareDistributedVirtualSwitch" `
-filter @{'Name'=$DistributedSwitch}`
-verbose:$false |
Select-Object -ExpandProperty MoRef|Select-Object -ExpandProperty Value
If ($Name)
{
$extraparams["filter"] = @{
'Name'=$Name
'Config.DistributedVirtualSwitch'="$($vDSMoRef)"
}
}
Else
{
$extraparams["filter"] = @{
'Config.DistributedVirtualSwitch'="$($vDSMoRef)"
}
}
}
If ($Name)
{
$extraparams["filter"] = @{'Name'=$Name}
}
}
Process
{
get-view -ViewType "DistributedVirtualPortgroup" -verbose:$false @extraparams |
Select-Object @{
Name='Name'
Expression={$_.Name}
},
@{
Name='Description'
Expression={$_.Config.Description}
},
@{
Name='PortBinding'
Expression={$_.Config.Type}
},
@{
Name='VLANID'
Expression={(($_.Config.DefaultPortConfig.Vlan.VlanId|%{
if ($_ -match "\d+") {$_}
elseIf ($_.Start -eq $_.End) {$_.Start}
Else {"{0}-{1}" -f $_.Start,$_.End}}) -join ",")}
},
@{
Name='NumbOfVMs'
Expression={$_.Vm.count}
},@{
Name='NumofPorts'
Expression={$_.PortKeys.count}
},
@{
Name='AlarmActions'
Expression={$_.AlarmActionsEnabled}
},
@{
Name='DistributedSwitch'
Expression={ Get-View $_.Config.DistributedVirtualSwitch `
-Property Name -verbose:$false |
Select-Object -ExpandProperty Name}
},
@{
Name='MoRef'
Expression={ $_.MoRef}
}
}
}
modified code
Function Get-DistributedSwitchPortGroup
{
<#
.SYNOPSIS
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.DESCRIPTION
Get Distributed Virtual Port Groups (DVPG) by name or vDS.
.PARAMETER Name
Name of the DVPG to retrieve supports wildcards.
.PARAMETER DistributedSwitch
Name of vDS to retrive the DVPG for.
.EXAMPLE
Get-DistributedSwitchPortGroup -Name PG02
.EXAMPLE
Get-DistributedSwitchPortGroup -DistributedSwitch vDS01
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true
, ValueFromPipeline=$true)]
[String]
$NAME
, [Parameter(Mandatory=$false
, ValueFromPipelineByPropertyName=$true)]
[String]
$DistributedSwitch
)
Begin
{
$extraparams=@{}
$extraparams["Property"] = @(
'Name'
, 'Config.Description'
, 'Config.Type'
, 'Config.DefaultPortConfig'
, 'Config.DistributedVirtualSwitch'
, 'VM'
, 'PortKeys'
, 'AlarmActionsEnabled'
)
IF ($DistributedSwitch)
{
$vDSMoRef = Get-view -Property Name `
-ViewType "VmwareDistributedVirtualSwitch" `
-filter @{'Name'=$DistributedSwitch}`
-verbose:$false |
Select-Object -ExpandProperty MoRef|Select-Object -ExpandProperty Value
If ($Name)
{
$extraparams["filter"] = @{
'Name'="^$($Name)$"
'Config.DistributedVirtualSwitch'="$($vDSMoRef)"
}
}
Else
{
$extraparams["filter"] = @{
'Config.DistributedVirtualSwitch'="$($vDSMoRef)"
}
}
}
If ($Name)
{
$extraparams["filter"] = @{'Name'="^$($Name)$"}
}
}
Process
{
get-view -ViewType "DistributedVirtualPortgroup" -verbose:$false @extraparams |
Select-Object @{
Name='Name'
Expression={$_.Name}
},
@{
Name='Description'
Expression={$_.Config.Description}
},
@{
Name='PortBinding'
Expression={$_.Config.Type}
},
@{
Name='VLANID'
Expression={(($_.Config.DefaultPortConfig.Vlan.VlanId|%{
if ($_ -match "\d+") {$_}
elseIf ($_.Start -eq $_.End) {$_.Start}
Else {"{0}-{1}" -f $_.Start,$_.End}}) -join ",")}
},
@{
Name='NumbOfVMs'
Expression={$_.Vm.count}
},@{
Name='NumofPorts'
Expression={$_.PortKeys.count}
},
@{
Name='AlarmActions'
Expression={$_.AlarmActionsEnabled}
},
@{
Name='DistributedSwitch'
Expression={ Get-View $_.Config.DistributedVirtualSwitch `
-Property Name -verbose:$false |
Select-Object -ExpandProperty Name}
},
@{
Name='MoRef'
Expression={ $_.MoRef}
}
}
}