VMware Cloud Community
RAJESH3311
Enthusiast
Enthusiast
Jump to solution

List VLans on all hosts

Dear All,

I need to find a way to display and export all vlans on all hosts. I am using Standard Switches. I require to have the table like this format.

HostName, VLanName, VirtualSwitchName, VLanID

I am able to get he details of individual hosts with the below command,

Get-VirtualPortGroup -VMHost hostname | ft Name, VirtualSwitch, VLanID

But the output does not show the Hostname.

I tried the command,

Get-VMHost | Get-VirtualPortGroup | ft Name, VirtualSwitch, VLanID

But still it is not showing the hostname on the output. If anybody know the PoweShell to create the required output, please help me out.

Regards

Rajesh

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sorry, missed a closing curly brace in the copy/paste.

The code above has been corrected, please try again.


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this, it should work for regular portgroups and portgroups on distributed switches.

foreach($esx in Get-VMHost){

    Get-VirtualPortGroup -VMHost $esx |

        Select @{N='VMHost';E={$esx.Name}},

            Name,

            @{N="VlanId";E={

                  if($_.ExtensionData -is [VMware.Vim.DistributedVirtualPortgroup]){

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

                      $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{

                          $_.Start.ToString() + "-" + $_.End.ToString()

                      }

                    }

                    elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId){

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

                    }

                    else{

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

                    }

                  }

                  else{

                    $_.VlanId

                  }}}}


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

RAJESH3311
Enthusiast
Enthusiast
Jump to solution

Dear LucD,

Thank you very much for the reply. But I am setting an error while running this script.

Please find the attached.

Regards

Rajesh

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, missed a closing curly brace in the copy/paste.

The code above has been corrected, please try again.


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

Reply
0 Kudos
RAJESH3311
Enthusiast
Enthusiast
Jump to solution

Dear LucD,

Great!!!

I really appriciate your efforts. The script worked.

Regards

Rajesh

Reply
0 Kudos
SurajVmware28
Contributor
Contributor
Jump to solution

Hi There , Can you help me to have complete this script not understanding where parenthesis missing 

 

 

Reply
0 Kudos
rjberube
Contributor
Contributor
Jump to solution

Many thanks for the script... Works beautifully in our setup... Output very informative.

BUT:

Is there a way to direct the screen output to a CSV fle, for easier slicing & dicing?

The iterative nature of the script [plus my newbie statue with PowerShell] is preventing easy use of Export-CSV.

Thanks in advance...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$report = foreach($esx in Get-VMHost){
    Get-VirtualPortGroup -VMHost $esx |
        Select @{N='VMHost';E={$esx.Name}},
            Name,
            @{N="VlanId";E={
                  if($_.ExtensionData -is [VMware.Vim.DistributedVirtualPortgroup]){
                    if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [Array]){
                      $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{
                          $_.Start.ToString() + "-" + $_.End.ToString()
                      }
                    }
                    elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId){
                      $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
                    }
                    else{
                      $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                    }
                  }
                  else{
                    $_.VlanId
                  }}}}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
rjberube
Contributor
Contributor
Jump to solution

One word: Fabulous!!!

This is a perfect, one-trick pony that helps me reconcile our VLAN documentation vs. actual usage.

Aside from customizing destination, this is/was ready-to-go.

Many thanks, and Kudos...


Reply
0 Kudos