VMware Cloud Community
nicholas1982
Hot Shot
Hot Shot
Jump to solution

List Portgroups with Private VLANS

Hi I was wondering if someone can help with listing all portgroups in a cluster or datacenter that are configured with Private VLAN so I want to ignore PG's with standard or no vlans?

Nicholas
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This is probably too much, but includes the connected VMs.

Get-VirtualPortGroup |

Select Name,

    @{N='Host';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            [string]::Join(',',(Get-View -Id $_.ExtensionData.Host -Property Name | %{$_.Name}))

        }

        else{

            Get-View -Id $_.VMHostId | Select -ExpandProperty Name

        }}},

    @{N='SwitchType';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            'VDS'

        }

        else{'VSS'}}},

    @{N='VLANType';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            switch($_.ExtensionData.Config.DefaultPortConfig.Vlan.GetType().Name){

            'VmwareDistributedVirtualSwitchPvlanSpec' {'PvLAN'}

            'mwareDistributedVirtualSwitchVlanIdSpec' {'VLAN'}

            'VmwareDistributedVirtualSwitchTrunkVlanSpec' {'VLAN Trunking'}

            Default {'None'}

            }

        }

        else{           

            $_.ExtensionData.GetType().Name

        }}},

    @{N='VlanId';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){

                $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId

            }

            elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]){

                if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]){

                    [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{"$($_.Start)-$($_.End)"}))

                }

                else{

                    $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId

                }

            }

        }

        else{$_.VlanId}}},

    @{N='Available ports';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            $_.numPorts - $_.ExtensionData.Vm.Count

        }

        else{'na'}}},

    @{N='Total ports';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            $_.NumPorts

        }

        else{'na'}}},

    @{N='VM';E={(Get-View -Id $_.ExtensionData.VM -Property Name | %{$_.Name}) -join '|'}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

8 Replies
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Ok I found the syntax to list all portgroups with Private VLANS but I would like to list VMs connected to those VLANS ?

Get-VDSwitch | Get-VDSwitchPrivateVlan

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is probably too much, but includes the connected VMs.

Get-VirtualPortGroup |

Select Name,

    @{N='Host';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            [string]::Join(',',(Get-View -Id $_.ExtensionData.Host -Property Name | %{$_.Name}))

        }

        else{

            Get-View -Id $_.VMHostId | Select -ExpandProperty Name

        }}},

    @{N='SwitchType';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            'VDS'

        }

        else{'VSS'}}},

    @{N='VLANType';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            switch($_.ExtensionData.Config.DefaultPortConfig.Vlan.GetType().Name){

            'VmwareDistributedVirtualSwitchPvlanSpec' {'PvLAN'}

            'mwareDistributedVirtualSwitchVlanIdSpec' {'VLAN'}

            'VmwareDistributedVirtualSwitchTrunkVlanSpec' {'VLAN Trunking'}

            Default {'None'}

            }

        }

        else{           

            $_.ExtensionData.GetType().Name

        }}},

    @{N='VlanId';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){

                $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId

            }

            elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]){

                if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]){

                    [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{"$($_.Start)-$($_.End)"}))

                }

                else{

                    $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId

                }

            }

        }

        else{$_.VlanId}}},

    @{N='Available ports';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            $_.numPorts - $_.ExtensionData.Vm.Count

        }

        else{'na'}}},

    @{N='Total ports';E={

        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){

            $_.NumPorts

        }

        else{'na'}}},

    @{N='VM';E={(Get-View -Id $_.ExtensionData.VM -Property Name | %{$_.Name}) -join '|'}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

This works well, what would i remove to filter out hosts as that info is not needed?

Cheers

Nicholas
Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

I was actually able to export to CSV the just delete the host column so its ok, the other this is it didn't actually filter out only PvLAN and listed or VDS portgroups so i added this to the end which seems to work well

?{$_.VLANType -like "PvLAN"} | Export-Csv -NoTypeInformation -UseCulture  c:\scripts\pvlan-vm.csv

The only think I wold like is to list VM's a little better but it will certainly do the job, thank you again for your help, much appreciated.

Cheers

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

How owuld you like to see the connected VMs?

I did a -join to get it all in one value.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

The VM list is fine, probably not many option here, we may have like 10 VMs on one PG and they kind of stitched together and a bit hard to read, possibly some more space between the VM names. If the -join was removed how would it format?

Cheers

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you leave out the -join, the property will be an array, and not behave well when you do an Export-Csv for example.

You can add space on the -join, for example: -join ' | '


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

Thanks I'll give that a try.

Cheers

Nicholas
Reply
0 Kudos