VMware Cloud Community
Troy_Clavell
Immortal
Immortal
Jump to solution

Cisco Discovery Protocol

How easy would it be to get the below codes output to a spreadsheet. Ideally each column be the name of the ESXi Host and under those columns are the returned results.

Get-VMHost | Where-Object {$_.State -eq "Connected"} |
%{Get-View $_.ID} |
%{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
%{ foreach($physnic in $_.NetworkInfo.Pnic){
    $pnicInfo = $_.QueryNetworkHint($physnic.Device)
    foreach($hint in $pnicInfo){
      Write-Host $esxname $physnic.Device
      if( $hint.ConnectedSwitchPort ) {
        $hint.ConnectedSwitchPort
      } else {
        Write-Host "No CDP information available."; Write-Host
      }
    }
  }
}

code found in http://kb.vmware.com/kb/1003885

1 Solution

Accepted Solutions
BenConrad
Expert
Expert
Jump to solution

This will give you CSV output where each row is a host with it's CDP properties

cls
$objReport = @()

function Get-CDP    {
    $obj = @()
    Get-VMHost  | Where-Object {$_.State -eq "Connected"} | %{Get-View $_.ID} |
    %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
    %{ foreach($physnic in $_.NetworkInfo.Pnic){
   
        $obj = "" | Select-Object hostname, pNic,PortId,Address,vlan       
   
        $pnicInfo = $_.QueryNetworkHint($physnic.Device)
        foreach($hint in $pnicInfo){
          #Write-Host "$esxname $($physnic.Device)"
          $obj.hostname = $esxname
          $obj.pNic = $physnic.Device
          if( $hint.ConnectedSwitchPort ) {
            # $hint.ConnectedSwitchPort
            $obj.PortId = $hint.ConnectedSwitchPort.PortId
          } else {
            # Write-Host "No CDP information available."; Write-Host
            $obj.PortId = "No CDP information available."
          }
          $obj.Address = $hint.ConnectedSwitchPort.Address
          $obj.vlan = $hint.ConnectedSwitchPort.vlan
         
        }
        $objReport += $obj
      }
    }
$objReport
}

$results = get-cdp
$results |  Export-Csv "c:\temp\out.csv"

View solution in original post

Reply
0 Kudos
10 Replies
BenConrad
Expert
Expert
Jump to solution

This will give you CSV output where each row is a host with it's CDP properties

cls
$objReport = @()

function Get-CDP    {
    $obj = @()
    Get-VMHost  | Where-Object {$_.State -eq "Connected"} | %{Get-View $_.ID} |
    %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
    %{ foreach($physnic in $_.NetworkInfo.Pnic){
   
        $obj = "" | Select-Object hostname, pNic,PortId,Address,vlan       
   
        $pnicInfo = $_.QueryNetworkHint($physnic.Device)
        foreach($hint in $pnicInfo){
          #Write-Host "$esxname $($physnic.Device)"
          $obj.hostname = $esxname
          $obj.pNic = $physnic.Device
          if( $hint.ConnectedSwitchPort ) {
            # $hint.ConnectedSwitchPort
            $obj.PortId = $hint.ConnectedSwitchPort.PortId
          } else {
            # Write-Host "No CDP information available."; Write-Host
            $obj.PortId = "No CDP information available."
          }
          $obj.Address = $hint.ConnectedSwitchPort.Address
          $obj.vlan = $hint.ConnectedSwitchPort.vlan
         
        }
        $objReport += $obj
      }
    }
$objReport
}

$results = get-cdp
$results |  Export-Csv "c:\temp\out.csv"

Reply
0 Kudos
Troy_Clavell
Immortal
Immortal
Jump to solution

Thanks BenConrad that should do the trick!

THe attachment is a bit different than your pasted code.  I used the pasted code to get the results.

Reply
0 Kudos
Pb981
Contributor
Contributor
Jump to solution

How can i add to this the "Device ID" string?

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean the PCI device hash ?

Like this

function Get-CDP    { 

    $objReport = @() 

    Get-VMHost | Where-Object {$_.State -eq "Connected"} | %{Get-View $_.ID} | 

    %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} | 

    %{ foreach($physnic in $_.NetworkInfo.Pnic){ 

     

        $obj = "" | Select-Object hostname, pNic,PCI,PortId,Address,vlan         

     

        $pnicInfo = $_.QueryNetworkHint($physnic.Device) 

        foreach($hint in $pnicInfo){ 

          #Write-Host "$esxname $($physnic.Device)" 

          $obj.hostname = $esxname 

          $obj.pNic = $physnic.Device

          $obj.PCI = $physnic.PCI

          if( $hint.ConnectedSwitchPort ) { 

            $obj.PortId = $hint.ConnectedSwitchPort.PortId 

          } else

            $obj.PortId = "No CDP information available." 

          } 

          $obj.Address = $hint.ConnectedSwitchPort.Address 

          $obj.vlan = $hint.ConnectedSwitchPort.vlan 

           

        } 

        $objReport += $obj 

      } 

    } 

$objReport 


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

vMarkusK1985
Expert
Expert
Jump to solution

Thanks to LucD​ for sharing this great function.

I have inetgrated the orignal version into my own Function:

function Get-UplinkDetails {

<#

    .NOTES

    ===========================================================================

    Created by: Markus Kraus

    Twitter: @VMarkus_K

    Private Blog: mycloudrevolution.com

    ===========================================================================

    Changelog: 

    2017.03 ver 1.0 Base Release 

    ===========================================================================

    External Code Sources:

    Get-CDP Version from @LucD22

    https://communities.vmware.com/thread/319553

    ===========================================================================

    Tested Against Environment:

    vSphere Version: vSphere 6.0 U2

    PowerCLI Version: PowerCLI 6.3 R1

    PowerShell Version: 4.0

    OS Version: Server 2012 R2

    Keyword: ESXi, Network, CDP, DVS, vSwitch, VMNIC

    ===========================================================================

    .DESCRIPTION

    This cmdlet collects detailed informations about your ESXi Host connections to pSwitch and DVS / vSwitch

    .Example

    Get-UplinkDetails -Clustername * | ft -AutoSize

    .Example

    Get-UplinkDetails -Clustername MyCluster001 | ft -AutoSize

    .Example

    Get-UplinkDetails -Clustername MyCluster* | Sort Clustername, Hostname, DVS | ft -AutoSize

    .PARAMETER Clustername

    Your vSphere Cluster Name or Wildcard

#Requires PS -Version 4.0

#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"}

#>

[CmdletBinding()]

param(

    [Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=0)]

        [String] $Clustername

       

)

Begin {

    $Validate = $True

    if (($myCluster = Get-Cluster -Name $Clustername).count -lt 1) {

       $Validate = $False

       throw "No Cluster '$myCluster' found"

    }

    if (($myHosts = $myCluster | Get-VMHost).count -lt 1) {

       $Validate = $False

       throw "No Hosts in Cluster '$myCluster' found"

    }

    function Get-CDP ($VMhost){

        $VMhostProxySwitch = $VMhost.NetworkInfo.ExtensionData.ProxySwitch

        $VMhostSwitch = $VMhost.NetworkInfo.VirtualSwitch

        $objReport = @()

        $VMhost| %{Get-View $_.ID} |

        %{ Get-View $_.ConfigManager.NetworkSystem} |

        %{ foreach($physnic in $_.NetworkInfo.Pnic){

    

            $obj = "" | Select-Object Clustername,Hostname,VMNIC,PCI,MAC,DVS,vSwitch,CDP_Port,CDP_Device,CDP_Address  

    

            $pnicInfo = $_.QueryNetworkHint($physnic.Device)

            foreach($hint in $pnicInfo){

                $obj.Clustername = $VMhost.parent.name

                $obj.Hostname = $VMhost.name

                $obj.VMNIC = $physnic.Device

                $obj.PCI = $physnic.PCI

                $obj.MAC = $physnic.Mac

                if ($backing = ($VMhostProxySwitch | where {$_.Spec.Backing.PnicSpec.PnicDevice -eq $physnic.Device}).DvsName) {

                    $obj.DVS = $backing

                    } else {

                        $obj.DVS = "-No Backing-"

                        }

                if ($backing = ($VMhostSwitch | where {$_.Nic -eq $physnic.Device}).Name) {

                    $obj.vSwitch = $backing

                    } else {

                        $obj.vSwitch = "-No Backing-"

                        }

                if( $hint.ConnectedSwitchPort ) {

                    $obj.CDP_Port = $hint.ConnectedSwitchPort.PortId

                    $obj.CDP_Device = $hint.ConnectedSwitchPort.DevId

                    $obj.CDP_Address = $hint.ConnectedSwitchPort.Address 

                    } else {

                        $obj.CDP_Port = "-No Info-"

                        $obj.CDP_Device = "-No Info-"

                        $obj.CDP_Address = "-No Info-"

                        }

               

            }

            $objReport += $obj

            }

        }

        $objReport

    }

 

}

Process {

    $MyView = @()

    if ($Validate -eq $True) {

 

        foreach ($myHost in $myHosts) {

        $CDP = Get-CDP $myHost

        $MyView += $CDP       

  }

          

       $MyView | Sort Clustername, Hostname, VMNIC

    }

    }

}

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
Reply
0 Kudos
goslackware3
Contributor
Contributor
Jump to solution

I've modified  vMarkusK1985 script, to include additional cdp fields in the below script:

swamynaveen
Enthusiast
Enthusiast
Jump to solution

goslackware3​ I would like to incorporate another two properties into vMarkusK1985  script like vmnic link status & vmnic's MTU size. Can you please with those two properties.

Reply
0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Thanks for sharing this great script.

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
Narayanan_5245
Enthusiast
Enthusiast
Jump to solution

I have tried to run this script but completing very quickly with no output? Can you suggest how to execute this function?

Copied the below script and Connected the center and executed it as below.

.\Get-UplinkDetails.ps1 -Clustername MyCluster001 | ft -AutoSize

regards

Narayanan.

Reply
0 Kudos
RKR28
Contributor
Contributor
Jump to solution

@Narayanan_5245 I am in same stage now.Can you please help me how to execute this.

Found the solution:

Added below line at the end of script to get the output

Get-UplinkDetails -Clustername * | Sort Clustername, Hostname, DVS | ft -AutoSize

===================================

However not getting vDS cdp details.If anyonce can help,would be highly appreciated

tried simiar trick by adding below line at end of script provided by @LucD  https://mycloudrevolution.com/en/2020/05/04/esxi-lldp-uplink-details/ but didnot work

Get-VMHost -Name MyHost | Get-UplinkDetails -Type CDP | Where-Object {$_.VDS -ne "-No Backing-"} | Format-Table -AutoSize

getting the below error

Get-VMHost VMHost with name 'MyHost' was not found using the specified filter(s).
At D:\RR\cdpinfo-v2.ps1:148 char:1

Reply
0 Kudos