Automation

 View Only
  • 1.  List VLans on all hosts

    Posted Apr 12, 2015 06:18 AM

    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



  • 2.  RE: List VLans on all hosts

    Posted Apr 12, 2015 02:36 PM

    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

                      }}}}



  • 3.  RE: List VLans on all hosts

    Posted Apr 13, 2015 04:59 AM

    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



  • 4.  RE: List VLans on all hosts
    Best Answer

    Posted Apr 13, 2015 05:04 AM

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

    The code above has been corrected, please try again.



  • 5.  RE: List VLans on all hosts

    Posted Apr 13, 2015 05:07 AM

    Dear LucD,

    Great!!!

    I really appriciate your efforts. The script worked.

    Regards

    Rajesh



  • 6.  RE: List VLans on all hosts

    Posted Sep 01, 2022 08:30 AM

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

     

     



  • 7.  RE: List VLans on all hosts

    Posted Sep 21, 2022 07:18 PM

    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...



  • 8.  RE: List VLans on all hosts

    Posted Sep 21, 2022 07:38 PM

    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
    


  • 9.  RE: List VLans on all hosts

    Posted Sep 22, 2022 07:51 PM

    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...