VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to sort Distributed Switch VLAN Info

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

ganapa2000_0-1678438382739.png

 

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am getting VLAN output as sorted but I am getting below error as few VLAN ID shows as VLAN Trunk

ganapa2000_0-1678450209624.png

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Perfect. That worked now. Thank you very much LucD. 🙂

 

Reply
0 Kudos