Leverage Cisco Discovery Protocol w/PowerCLi

Cover

If you are a lucky owner of Cisco switches, the Get-VMHostCDP function will give you all the necessary information about the network settings using the Cisco Discovery Protocol.

Get-VMHostCDP

  • The CDP info for a single network adapter of ESXi host can be retrieved from GUI.

01.Get-VMHostCDP_WebClient

  • The Get-VMHostCDP function from my PowerCLi Vi-Module module retrieves the CDP info for all existing network adapters of any ESXi host(s) and some important ESXi side info. By default, the function returns necessary properties only.
Get-VMHost | Get-VMHostCDP

02.Get-VMHostCDP_Default

  • The default behavior can be easy changed by -CdpOnly parameter. Only CDP capable ports will be returned. If no output is returned, probably the CDP is disabled either on the switch or on particular ports.
Get-VMHost | Get-VMHostCDP –CdpOnly

03.Get-VMHostCDP_CdpOnly

  • To return all or select any properties you want, use the Select-Object cmdlet.
Get-VMHost esx1.* | Get-VMHostCDP -CdpOnly | select *
Get-VMHost esx1.* | Get-VMHostCDP -CdpOnly | select * -exclude mgmt.*
Get-VMHost esx1.* | Get-VMHostCDP -CdpOnly | select NIC, Switch, mgmt*, PortId, Vlan

04.Get-VMHostCDP_Select

  • The Get-VMHostCDP function returns custom [ViCDP] object. It has three static methods: ToString(), GetVlan(Id) and GetAllVlan().
Get-VMHost | Get-VMHostCDP | Get-Member

05.Get-VMHostCDP_Methods

Method ToString()

  • This method gives you brief port-to-port view.
Get-Cluster prod | Get-VMHost | Get-VMHostCDP -CdpOnly | % { $_.ToString() }
  • Just converting the output object to the [string] data type calls the ToString() method in the background and returns the same result.
Get-Cluster prod | Get-VMHost | Get-VMHostCDP -CdpOnly | % { [string]$_ }

06.Get-VMHostCDP_ToString

Method GetVlan(Id)

  • This method is a VLAN searcher. It allows to find on which port(s) particular VLAN(s) are set. It can search either a single VLAN, several VLANs or any VLAN range.
Get-Cluster prod | Get-VMHost | Get-VMHostCDP -CdpOnly | % { $_.GetVlan(25) } | ft –au
Get-Cluster prod | Get-VMHost | Get-VMHostCDP -CdpOnly | % { $_.GetVlan((23,25)) } | ft –au
Get-Cluster prod | Get-VMHost | Get-VMHostCDP -CdpOnly | % { $_.GetVlan(23..25) } | ft -au

07.Get-VMHostCDP_GetVlan

  • Please note when you pass to the method a comma delimited VLAN list, additional parentheses are needed GetVlan((20,30,40)) to recognize a list as a single argument.

08.Get-VMHostCDP_GetVlan

Method GetAllVlan()

  • This method gives you vlan-to-port report. It expands the Vlan property from the original object. The GetAllVlan() method especially useful to find port configuration inconsistency within HA/DRS Clusters.
Get-VMHost esx1.* | Get-VMHostCDP -CdpOnly | % { $_.GetAllVlan() } | ft –au
Get-Cluster prod | Get-VMHost | Get-VMHostCDP -CdpOnly | % { $_.GetAllVlan() } | epcsv –notype .\Vlan.csv

 

09.Get-VMHostCDP_GetAllVlan

The CISCO side

  • Despite the CDP is enabled by default on the Cisco devices, may happen it was disabled for any reason on your switch. For completeness I decided to write a short briefing how to enable the CDP back.

  • Connect to the switch, enter to Enable mode to view global CDP status. Why global? The reason is the CDP have to be enabled on two levels: globally on the device level and per port.

ena
sh cdp
  • Enter to the Global Configuration mode and enable CDP on the switch.
conf t
cdp run
  • Review CDP status on particular port or on all switch ports.
do sh cdp int e0/1
do sh cdp int
  • Enter to Interface Configuration mode by selecting either a single port, port range where your ESXI hosts are connected or all switch ports and enable the CDP on these ports.
int f0/1
int range g0/1-10, e1/1-4
int range g0/1-24
cdp ena
  • To persist the changes you made, copy running to startup configuration.
do copy run start

10.Get-VMHostCDP_PacketTracer

  • All done. Now there is no reason to get an empty output while using -CdpOnly parameter.

Summary

  • The CDP is a great enhancement that helps to VMware administrators to troubleshot network issues especially in enterprises where Networking and System teams are separated.
  • For more details about the function, please take a look at the content based help and examples.

Get-Alias –Definition Get-VMHostCDP
Get-Help Get-VMHostCDP -Full
Get-Help Get-VMHostCDP -Examples
Get-Help Get-VMHostCDP -Parameter CdpOnly

You might also like

Get-VMHostFirmwareVersion – Get ESXi servers BIOS/Firmware version
Compare-VMHost – Compare two or more ESXi hosts
Get-ViSession/Disconnect-ViSession – List and disconnect VCenter sessions
Search-Datastore – Browse/Search VMware Datastores

10 thoughts on “Leverage Cisco Discovery Protocol w/PowerCLi

      1. You can use calculated property:

        Get-VMHost | Get-VMHostCDP -CdpOnly | select *, @{N='Parent'; E={(Get-VMHost "$($_.VMHost).*").Parent.Name}}
        

        Like

  1. Do you also have the problem, that not all VLAN (which are on the Port configured) are listed in the result? How could i fix this?

    Like

      1. Do you have Powered on VM, connected to appropriate PortGroups, configured for those VLAN ? If no, try to vMotion any VM to the hosts that do not see some VLAN. I had this problem too for non VMkernel VLANs…

        Like

Leave a comment