VMware ESXi   CDP information via PowerCLI

VMware ESXi   CDP information via PowerCLI

  1. Using PowerCLI, connect to the ESX host or vCenter Server:# Connect-VIServer esx-or-vc-hostname
  2. Run this script in PowerCLI:
Get-VMHost | Where-Object {$_.ConnectionState -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
      }
    }
  }
}

The CDP information for each physical network interface on each connected ESX host is displayed.

For example:

 

 

 

 

 

 

 

To get more information about the uplink Cisco switch and related configured physical switch ports with CDP, run this script in PowerCLI:

param($VMHost)
$vmh = Get-VMHost $VMHost
If ($vmh.State -ne "Connected") {
  Write-Output "Host $($vmh) state is not connected, skipping."
  }
Else {
  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.ConnectedSwitchPort ) {
        $hint.ConnectedSwitchPort | select @{n="VMHost";e={$esxname}},@{n="VMNic";e={$physnic.Device}},DevId,Address,PortId,HardwarePlatform
        }
      else {
        Write-Host "No CDP information available."
        }
      }
    }
  }
}

For example, if this script is saved as Get-mVMHostCDPInfo.ps1 :

# Get-mVMHostCDPInfo.ps1 -VMHost esxhostname | ft -a

VMHost VMNic DevId Address PortId HardwarePlatform
—— —– —– ——- —— —————-
esxhostname vmnic0 esx1.mon.com 1.1.1.1 GigabitEthernet1/0/9 cisco WS-C3750G-24TS-1U
esxhostname vmnic1 esx1.mon.com 1.1.1.1 GigabitEthernet2/0/21 cisco WS-C3750G-24TS-1U
esxhostname vmnic2 esx1.mon.com 1.1.1.1 GigabitEthernet1/0/10 cisco WS-C3750G-24TS-1U
esxhostname vmnic3 esx1.mon.com 1.1.1.1 GigabitEthernet2/0/10 cisco WS-C3750G-24TS-1U
esxhostname vmnic4 esx1.mon.com 1.1.1.1 GigabitEthernet1/0/11 cisco WS-C3750G-24TS-1U
esxhostname vmnic5 esx1.mon.com 1.1.1.1 GigabitEthernet2/0/11 cisco WS-C3750G-24TS-1U
esxhostname vmnic6 esx2.mon.com 1.1.1.2 GigabitEthernet1/0/6 cisco WS-C3750G-24TS-1U
esxhostname vmnic7 esx2.mon.com 1.1.1.2 GigabitEthernet1/0/8 cisco WS-C3750G-24TS-1U

Source link     download script

Your email address will not be published. Required fields are marked *