juststormY
Contributor
Contributor

Get CDP infos via PowerShell

Jump to solution

Hello all together,

i'm searching for something really special. I hope anybody can help me to solve this in here.

I want a script (powershell) that shows me for every ESX host in a VC the CDP (Cisco Discovery Protocol) Information of the vmnics.

Seems to be a really big problem to get this. I allready tried to get theese informations through the ESX hosts themselves. But it seems that not the ESX hosts retrieve this information but rather the VC. Right now i'm able to get the information on which vmnic CDP is enabled but not the information like Switchname, Switchport,.....

Do you have any ideas to get this? This would help me much! Thank you for helping!

1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee

Here's one I made earlier...


Connect-VIServer MYVISERVER
Write "Gathering VMHost objects"
$vmhosts = Get-VMHost | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View
$MyCol = @()
foreach ($vmhost in $vmhosts){
 $ESXHost = $vmhost.Name
 Write "Collating information for $ESXHost"
 $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
 foreach($pnic in $networkSystem.NetworkConfig.Pnic){
     $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)
     foreach($Hint in $pnicInfo){
         $NetworkInfo = "" | select-Object Host, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN
         $NetworkInfo.Host = $vmhost.Name
         $NetworkInfo.PNic = $Hint.Device
         $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId
         $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
         $record = 0
         Do{
             If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
                 $NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
                 $NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac
             }
             $record ++
         }
         Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
         foreach ($obs in $Hint.Subnet){
             $NetworkInfo.Observed += $obs.IpSubnet + " "
             Foreach ($VLAN in $obs.VlanId){
                 If ($VLAN -eq $null){
                 }
                 Else{
                     $strVLAN = $VLAN.ToString()
                     $NetworkInfo.VLAN += $strVLAN + " "
                 }
             }
         }
         $MyCol += $NetworkInfo
     }
 }
}
$Mycol | Sort Host, PNic 

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

VMware, Citrix, Microsoft Consultant

UK

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

7 Replies
alanrenouf
VMware Employee
VMware Employee

Here's one I made earlier...


Connect-VIServer MYVISERVER
Write "Gathering VMHost objects"
$vmhosts = Get-VMHost | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View
$MyCol = @()
foreach ($vmhost in $vmhosts){
 $ESXHost = $vmhost.Name
 Write "Collating information for $ESXHost"
 $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
 foreach($pnic in $networkSystem.NetworkConfig.Pnic){
     $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)
     foreach($Hint in $pnicInfo){
         $NetworkInfo = "" | select-Object Host, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN
         $NetworkInfo.Host = $vmhost.Name
         $NetworkInfo.PNic = $Hint.Device
         $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId
         $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
         $record = 0
         Do{
             If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
                 $NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
                 $NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac
             }
             $record ++
         }
         Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
         foreach ($obs in $Hint.Subnet){
             $NetworkInfo.Observed += $obs.IpSubnet + " "
             Foreach ($VLAN in $obs.VlanId){
                 If ($VLAN -eq $null){
                 }
                 Else{
                     $strVLAN = $VLAN.ToString()
                     $NetworkInfo.VLAN += $strVLAN + " "
                 }
             }
         }
         $MyCol += $NetworkInfo
     }
 }
}
$Mycol | Sort Host, PNic 

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

VMware, Citrix, Microsoft Consultant

UK

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
LucD
Leadership
Leadership

That question already came up a couple of times.

Have a look at and


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

juststormY
Contributor
Contributor

Hello both,

thanks, thats what i wanted.

Sorry for my bad searching before but i just don't get it.

Thanks a lot for the relly fast help!

0 Kudos
juststormY
Contributor
Contributor

@alanrenouf

Just one question to your script: Is it possible to write also the vSwitch of each pnic into the output?

I've done some testing now but seems that i have a mistake in my idea.

Thanks!

0 Kudos
alanrenouf
VMware Employee
VMware Employee

Sure, this should do it...


Connect-VIServer MYVISERVER
$filename = "c:\DetailedNetworkInfo.csv"

Write "Gathering VMHost objects"
$vmhosts = Get-VMHost | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View
$MyCol = @()
foreach ($vmhost in $vmhosts){
    $ESXHost = $vmhost.Name
    Write "Collating information for $ESXHost"
    $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
    foreach($pnic in $networkSystem.NetworkConfig.Pnic){
        $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)
        foreach($Hint in $pnicInfo){
            $NetworkInfo = "" | select-Object Host, vSwitch, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN
            $NetworkInfo.Host = $vmhost.Name
            $NetworkInfo.vSwitch = Get-Virtualswitch -VMHost (Get-VMHost ($vmhost.Name)) | where {$_.Nic -eq ($Hint.Device)}
            $NetworkInfo.PNic = $Hint.Device 
            $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId 
            $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
            $NetworkInfo.VLAN = $Hint.connectedSwitchPort.Vlan
            $record = 0
            Do{
                If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
                    $NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
                    $NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac
                }
                $record ++
            }
            Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
            foreach ($obs in $Hint.Subnet){
                $NetworkInfo.Observed += $obs.IpSubnet + " "
            }
            $MyCol += $NetworkInfo
        }
    }
}
$Mycol | Sort Host, PNic | Export-Csv $filename -NoTypeInformation
 


If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

VMware, Citrix, Microsoft Consultant

UK

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
wamatha
Contributor
Contributor

Hi

I get this error when I run this script,

Gathering VMHost objects

Collating information for

The argument cannot be null or empty.

0 Kudos
Sudarshan_Bhart
Enthusiast
Enthusiast

Hello Alan

Below mentioned line is not working for me. No information for this column in the report.

$NetworkInfo.vSwitch = Get-Virtualswitch -VMHost (Get-VMHost ($vmhost.Name)) | where {$_.Nic -eq ($Hint.Device)} 

Any suggestion on this will be highly appreciated.

Thanks

Sudarshan Bharti

0 Kudos