7 Replies Latest reply on Apr 18, 2019 9:25 AM by LucD

    Collect vDS Portgroup, VMNIC and CDP information

    faherne_CTI Novice

      Hi,

       

      I found an awesome PowerCLI function that works fantastically, but I'd really like to get a slightly modified table output that included the following:

       

      | VMHost | PortGroup Name | VMNIC ID | VLAN No. | Switch Port ID |  Switch Port ID Connected |  Switch Name | Switch VLAN IP |

       

      The following article gives me almost all of the above except:

      • The vDS Portgroup Name
      • The vDS PortGroup VLAN number
      • Take into account that a VMNIC may be a member of more than 1 portgroup

       

      https://www.jonathanmedd.net/2013/07/obtaining-cdp-info-via-powercli.html

      I did find a different PowerCLI script that pulls the vDS portgroup VLAN, but doesn't include the configured PortGroup Name, and doesn't take into account that a VMNIC may be a member of more than one PortGroup: Cisco Discovery Protocol

       

      Here's my slightly modified code, but I do not know how to integrate the 3 bullet points above:

       

      ####################################################################################

       

      function Get-VMHostNetworkAdapterCDP {
      <#
      .SYNOPSIS
      Function to retrieve the Network Adapter CDP info of a vSphere host.

      .DESCRIPTION
      Function to retrieve the Network Adapter CDP info of a vSphere host.

      .PARAMETER VMHost
      A vSphere ESXi Host object

      .INPUTS
      System.Management.Automation.PSObject.

      .OUTPUTS
      System.Management.Automation.PSObject.

      .EXAMPLE
      PS> Get-VMHostNetworkAdapterCDP -VMHost ESXi01,ESXi02

      .EXAMPLE
      PS> Get-VMHost ESXi01,ESXi02 | Get-VMHostNetworkAdapterCDP
      #>

      [CmdletBinding()][OutputType('System.Management.Automation.PSObject')]

      Param
      (

      [parameter(Mandatory=$true,ValueFromPipeline=$true)]
      [ValidateNotNullOrEmpty()]
      [PSObject[]]$VMHost
      )

      begin {

      $ErrorActionPreference = 'Stop'
      Write-Debug $MyInvocation.MyCommand.Name
      $CDPObject = @()
      }

      process{

      try {
      foreach ($ESXiHost in $VMHost){

      if ($ESXiHost.GetType().Name -eq "string"){

      try {
      $ESXiHost = Get-VMHost $ESXiHost -ErrorAction Stop
      }
      catch [Exception]{
      Write-Warning "VMHost $ESXiHost does not exist"
      }
      }

      elseif ($ESXiHost -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]){
      Write-Warning "You did not pass a string or a VMHost object"
      Return
      }

      $ConfigManagerView = Get-View $ESXiHost.ExtensionData.ConfigManager.NetworkSystem
      $PNICs = $ConfigManagerView.NetworkInfo.Pnic

      foreach ($PNIC in $PNICs){

      $PhysicalNicHintInfo = $ConfigManagerView.QueryNetworkHint($PNIC.Device)

      if ($PhysicalNicHintInfo.ConnectedSwitchPort){

      $Connected = $true
      }
      else {
      $Connected = $false
      }

      $hash = @{
      MangementAddress = $PhysicalNicHintInfo.ConnectedSwitchPort.MgmtAddr
      Switch = $PhysicalNicHintInfo.ConnectedSwitchPort.DevId
      Connected = $Connected
      PortId = $PhysicalNicHintInfo.ConnectedSwitchPort.PortId
      NIC = $PNIC.Device
      VMHost = $ESXiHost.Name
      #HardwarePlatform = $PhysicalNicHintInfo.ConnectedSwitchPort.HardwarePlatform
      #SoftwareVersion = $PhysicalNicHintInfo.ConnectedSwitchPort.SoftwareVersion
      }
      $Object = New-Object PSObject -Property $hash
      $CDPObject += $Object
      }
      }
      }
      catch [Exception] {

      throw "Unable to retrieve CDP info"
      }
      }
      end {

      Write-Output $CDPObject
      }
      }

       

      $MyCluster = "<Insert Clustername>"

      Get-Cluster $MyCluster| Get-VMHost | Get-VMHostNetworkAdapterCDP | Select VMHost,NIC,PortId,Connected,Switch,MangementAddress | Export-Csv .\$($MyCluster)"_CDP_Info.csv" -NoTypeInformation -UseCulture

       

      ##################################################################################################

        • 1. Re: Collect vDS Portgroup, VMNIC and CDP information
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          I'm not sure I understand what you are trying to do here.
          A pNIC is connected to a Switch (VSS or VDS), not a portgroup.
          The only link you can establish is to list all portgroups on that switch.

          • 2. Re: Collect vDS Portgroup, VMNIC and CDP information
            faherne_CTI Novice

            A VMNIC is a label for a pNIC, right? e.g. vmnic0

             

            I'm looking to create a table in terms of the active and standby VMNICs/pNICs assigned to each portgroup in a vDS, and then correlate this information with the CDP info pulled from the physical switches and their ports attached to the pNICs.

            • 3. Re: Collect vDS Portgroup, VMNIC and CDP information
              faherne_CTI Novice

              E.g.

               

              - An ESXi host has 8 pNICs tagged vmnic0-7.

              - Each ESXi Host is connected to 2 x physical switches, 4 ports to Switch01 (odd numbered0), 4 ports to Switch-02 (even numbered)

              - Each host has a single vDS assigned with the 8 pNICs.

              - Each port group has 4 x Portgroups:

              1. Mgmt:

              Active: vmnic0, Standby: vmnic1

              2. vMotion:

              Active: vmnic1, Standby: vmnic0

              3. VM Network

              Active: vmnic2,3,4,5

              4. NFS

              Active: vmnic6, Standby: vmnic7

               

              - I need to display this information to the Networks team, so that they can correlate the different ESXi network traffic to their Cisco switch ports.

              • 4. Re: Collect vDS Portgroup, VMNIC and CDP information
                faherne_CTI Novice

                I'm looking to create a table as follows:

                 

                | VMHost | PortGroup Name | VMNIC ID | VLAN No. | Switch Port ID |  Switch Port ID Connected |  Switch Name | Switch VLAN IP  |

                | ESX01    | Mgmt                     | vmnic0      | VLAN10    | Eth100/1/01      |  Connected                           | Switch01         |  192.168.10.1    |

                | ESX01    | Mgmt                     | vmnic1      | VLAN10    | Eth101/1/01      |  Connected                           | Switch02         |  192.168.11.1    |

                | ESX01    | vMotion                 | vmnic1      | VLAN20    | Eth101/1/02      |  Connected                           | Switch02         |  192.168.20.1    |

                | ESX01    | vMotion                 | vmnic0      | VLAN20    | Eth100/1/02      |  Connected                           | Switch01         |  192.168.21.1    |

                | ESX01    | VM network           | vmnic2      | VLAN30    | Eth100/1/03      |  Connected                           | Switch01         |  192.168.30.1    |

                 

                | ESX01    | VM network           | vmnic3      | VLAN30    | Eth101/1/03      |  Connected                           | Switch02         |  192.168.31.1    |

                | ESX01    | VM network           | vmnic4      | VLAN30    | Eth100/1/04      |  Connected                           | Switch01         |  192.168.30.2    |

                | ESX01    | VM network           | vmnic5      | VLAN30    | Eth101/1/04      |  Connected                           | Switch02         |  192.168.31.2    |

                | ESX01    | NFS                       | vmnic6      | VLAN40    | Eth100/1/05      |  Connected                           | Switch01         |  192.168.40.1    |

                | ESX01    | NFS                       | vmnic7      | VLAN40    | Eth101/1/05      |  Connected                           | Switch01         |  192.168.41.1    |

                • 5. Re: Collect vDS Portgroup, VMNIC and CDP information
                  LucD Guru
                  vExpertUser ModeratorsCommunity Warriors

                  Ok, you didn't mention NIC Teaming before.
                  Try something like this

                   

                  Get-Cluster -PipelineVariable cluster |

                  Get-VMHost -PipelineVariable esx |

                  ForEach-Object -Process {

                     Get-VirtualPortGroup -VMHost $esx -Standard -PipelineVariable pg |

                     ForEach-Object -Process {

                     @($pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic) +

                     @($pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandbyNic) +

                     @($pg.VirtualSwitch.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic) +

                     @($pg.VirtualSwitch.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandbyNic) |

                     Sort-Object -Unique |

                     ForEach-Object -Process {

                     $netSys.QueryNetworkHint($_) |

                     ForEach-Object -Process {

                     $hint = $_

                     $hint.ConnectedSwitchPort |

                     ForEach-Object -Process {

                     New-Object PSObject -Property ([ordered]@{

                     Cluster = $cluster.Name

                     VMHost = $esx.Name

                     Switch = $pg.VirtualSwitch.Name

                     PortGroup = $pg.Name

                     VLanId = $pg.VLanId

                     Vmnic = $hint.Device

                     IP = $_.Address

                     SwitchPort = $_.PortId

                     SwitchPortVLanId =  $_.VLan

                     })

                     }

                     }

                     }

                     }

                  }

                  • 6. Re: Collect vDS Portgroup, VMNIC and CDP information
                    faherne_CTI Novice

                    Hi Luc,

                     

                    Is it complicated to modify the above to query an ESXi host with a vDS configured?

                     

                     

                     

                    • 7. Re: Collect vDS Portgroup, VMNIC and CDP information
                      LucD Guru
                      User ModeratorsCommunity WarriorsvExpert

                      VDS is a completely different story.
                      I'll have a look.