7 Replies Latest reply on Jul 25, 2013 11:07 AM by RKDRnD

    How Get VLAN ID with Get-VirtualPortGroup

    rogermccarrick Novice

      I have vsphere with a number of distributed virtual switches.

      I have a number of port groups on each dvswitch.

      I want to get the names and vlan-IDs of all the port groups.

       

      I tried:

       

      Get-VirtualSwitch -Name MyDVSwitch | Get-VirtualPortGroup | Select Name, Vlanid

       

      it returns a list of the port group names and just blank for the VLANID. I was expecting to see:

       

      Name     Vlanid

      --------      ---------

      mypg-1   23

      mypg-2   17

      mypg-3   48

       

      and so on. But I just get:

       

      Name     Vlanid

      --------      ---------

      mypg-1  

      mypg-2  

      mypg-3  

       

      any ideas? Also I'd prefer to have a script that doesnt have to specify the dvswitch by name, but rather found all the port groups on all the dvswitches on all the clusters in the data center and listed by name and vlan-id.

       

      thanks

        • 1. Re: How Get VLAN ID with Get-VirtualPortGroup
          bulletprooffool Virtuoso
          User Moderators

          Odd  -  should be OK?

           

          try this:

           

          Get-VirtualPortGroup | ?{$_.VirtualSwitchName -eq "mySwitch"} | select VLanId, Name
          • 2. Re: How Get VLAN ID with Get-VirtualPortGroup
            LucD Guru
            Community WarriorsUser ModeratorsvExpert

            You're obvisouly looking at portgroups on distributed switches, for those the VlanId property is not present.

            You can do

            Get-VirtualSwitch -Name MyDVSwitch | Get-VirtualPortGroup | `

            Select Name, @{N="VLANId";E={$_.Extensiondata.Config.DefaultPortCOnfig.Vlan.VlanId}}


            Note that this does not work for the uplink portgroup.

            • 3. Re: How Get VLAN ID with Get-VirtualPortGroup
              bulletprooffool Virtuoso
              User Moderators

              Running your code (and mine) it seems as if it throws a bunch of white space before each 'VLANID'

               

               

              so if you try :

               

              Get-VirtualSwitch -Name MyDVSwitch | Get-VirtualPortGroup | Select Name, Vlanid | ft -au

               

              You should get what you are looking for too . .

               

              Assuming you are not look at a DV Switch (which the switch name may indicate you are . . )

              • 4. Re: How Get VLAN ID with Get-VirtualPortGroup
                halr9000 Master

                There are some weird technical reasons why vmw didn't do this out of the gate, but I came across this the other day and reported it to them.  I made an enhancement request that they add the vlan id's for dvportgroups, and their response was promising. Hopefully we will see this in a not-too-distant future version.

                • 5. Re: How Get VLAN ID with Get-VirtualPortGroup
                  rogermccarrick Novice

                  thanks ... this seems to work.

                  Before the "Select" statement there is a "`". What's that for? It works with or without it.

                   

                  I changed the order of select and piped to a txt file.

                   

                  Get-VirtualSwitch -Name Storage | Get-VirtualPortGroup | select @{N="VLANId";E={$_.Extensiondata.Config.DefaultPortCOnfig.Vlan.VlanId}}, name >> vlans2.txt

                   

                  I get:

                   


                                                   VLANId Name
                                                        ------ ----
                                                       72 mypg-1
                                  VMware.Vim.NumericRange Storage-DVUplinks-157
                                                        0 Storage
                                                       64 mypg-2
                                                        6 mypg-3

                   

                  the formatting is a bit off but I can import to excel.

                   

                  Get-VirtualSwitch -Name MyDVSwitch | Get-VirtualPortGroup | Select Name, Vlanid | ft -au still gives a blank vlanid.

                   

                  thanks again!

                  • 6. Re: How Get VLAN ID with Get-VirtualPortGroup
                    LucD Guru
                    User ModeratorsvExpertCommunity Warriors

                    The back-tick is a line continuation character.

                    Something you can use if you want split a long line over multiple lines.

                    • 7. Re: How Get VLAN ID with Get-VirtualPortGroup
                      RKDRnD Novice

                      Get-VirtualSwitch -Name MyDVSwitch | Get-VirtualPortGroup | `

                      Select Name, @{N="VLANId";E={$_.Extensiondata.Config.DefaultPortCOnfig.Vlan.VlanId}}

                       

                      This has worked for me on vSphere 5.1. But does not pick up vlanid parameter on 4.1. Is that expected?