Hi,
I am trying to get the Portgroup Info along with its VLAN information. But in the output VLAN information is not getting sorted properly
Please help!!
Get-VDPortgroup | sort VlanConfiguration | Select VDSwitch, Name, VlanConfiguration | ft -auto
If the sort can be done on the 1st range, you could try
Get-VDPortgroup |
Sort-Object -Property {
if($_.VlanConfiguration -eq $null){
$null
}
elseif ($_.VlanConfiguration.GetType().Name -eq 'SingleVlanConfigurationImpl') {
[int]$_.VlanConfiguration.VlanId
}
else{
if($_.VlanConfiguration.Ranges.Count -eq 1){
[int]$_.VlanConfiguration.Ranges.StartVlanId * 4095 + [int]$_.VlanConfiguration.Ranges.EndVlanId
}
else{
[int]$_.VlanConfiguration.Ranges[0].StartVlanId * 4095 + [int]$_.VlanConfiguration.Ranges[0].EndVlanId
}
}
} |
Select VDSwitch, Name, VlanConfiguration |
Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
Get-VDPortgroup |
Sort-Object -Property {[int]($_.VlanConfiguration.Split(' ')[1])} |
Select VDSwitch, Name, VlanConfiguration |
Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Getting below error and output without any sorting
Sort-Object : Method invocation failed because [VMware.VimAutomation.Vds.Impl.V1.SingleVlanConfigurationImpl] does not contain a method named 'Split'.
At line:1 char:19
+ ... Portgroup | Sort-Object -Property {[int]($_.VlanConfiguration.Split(' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (2203dvPortGroup:PSObject) [Sort-Object], RuntimeException
+ FullyQualifiedErrorId : ExpressionEvaluation,Microsoft.PowerShell.Commands.SortObjectCommand
Yes, that method assumes all VLAN definition are of the Single VLAN type.
Try this method, it allows for trunked VLANs, single VLANs, and no VLANs.
The portgroups with no VLAN defined will appear first in the output.
Get-VDPortgroup |
Sort-Object -Property {
if($_.VlanConfiguration -eq $null){
$null
}
elseif ($_.VlanConfiguration.GetType().Name -eq 'SingleVlanConfigurationImpl') {
[int]$_.VlanConfiguration.VlanId
}
else{
[int]$_.VlanConfiguration.Ranges.StartVlanId * 4095 + [int]$_.VlanConfiguration.Ranges.EndVlanId
}
} |
Select VDSwitch, Name, VlanConfiguration |
Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I am getting VLAN output as sorted but I am getting below error as few VLAN ID shows as VLAN Trunk
That is because the code doesn't handle multiple ranges yet.
In fact how do you sort those?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If the sort can be done on the 1st range, you could try
Get-VDPortgroup |
Sort-Object -Property {
if($_.VlanConfiguration -eq $null){
$null
}
elseif ($_.VlanConfiguration.GetType().Name -eq 'SingleVlanConfigurationImpl') {
[int]$_.VlanConfiguration.VlanId
}
else{
if($_.VlanConfiguration.Ranges.Count -eq 1){
[int]$_.VlanConfiguration.Ranges.StartVlanId * 4095 + [int]$_.VlanConfiguration.Ranges.EndVlanId
}
else{
[int]$_.VlanConfiguration.Ranges[0].StartVlanId * 4095 + [int]$_.VlanConfiguration.Ranges[0].EndVlanId
}
}
} |
Select VDSwitch, Name, VlanConfiguration |
Format-Table -AutoSize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect. That worked now. Thank you very much LucD. 🙂