Automation

 View Only
  • 1.  Get CDP infos via PowerShell

    Posted Feb 13, 2009 01:23 PM

    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!



  • 2.  RE: Get CDP infos via PowerShell
    Best Answer

    Broadcom Employee
    Posted Feb 13, 2009 01:28 PM

    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



  • 3.  RE: Get CDP infos via PowerShell

    Posted Feb 13, 2009 02:51 PM

    @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!



  • 4.  RE: Get CDP infos via PowerShell

    Posted Feb 13, 2009 01:29 PM

    That question already came up a couple of times.

    Have a look at and



  • 5.  RE: Get CDP infos via PowerShell

    Posted Feb 13, 2009 01:41 PM

    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!



  • 6.  RE: Get CDP infos via PowerShell

    Broadcom Employee
    Posted Feb 13, 2009 04:39 PM

    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



  • 7.  RE: Get CDP infos via PowerShell

    Posted Feb 18, 2009 03:29 PM

    Hi

    I get this error when I run this script,

    Gathering VMHost objects

    Collating information for

    The argument cannot be null or empty.



  • 8.  RE: Get CDP infos via PowerShell

    Posted Mar 24, 2015 01:19 AM

    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