Wednesday, May 22, 2013

Using VMware PowerCLI to get LLDP switch information

This script is based on a CDP version on VMware’s site. CDP is used by Cisco, but most other vendors use LLDP.  The LLDPinfo object is setup differently from the CDP object.  Hopefully this will save a lot of time clicking thru the GUI.

$vmh = Get-VMHost | sort
If ($vmh.State -eq "Connected")
{
  Get-View $vmh.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.LLDPInfo ) {
        $results = New-Object –TypeName PSObject
        $results | Add-Member –MemberType NoteProperty –Name ESXHostName –Value $esxname
        $results | Add-Member –MemberType NoteProperty –Name DeviceName –Value $physnic.Device
        $results | Add-Member –MemberType NoteProperty –Name PortName –Value $hint.LldpInfo.Parameter[4].value
        $results | Add-Member –MemberType NoteProperty –Name SwitchName –Value $hint.LldpInfo.Parameter[6].value
        Write-Output $results
        }
       }
    }
  }
}

1 comment:

  1. By using this script we get the following message:
    WARNING: The 'State' property of VMHost type is deprecated. Use the
    'ConnectionState' property instead.

    After changing $vmh.State to $vmh.ConnectionState we get the same output, but no error message. :)

    Lars

    ReplyDelete