Monday, September 7, 2015

Powercli Pull CDP and LLDP information in single nice table format - Part 2


Today I received newsletter email from psvmware.wordpress.com, He has written nice blog subject Get-VMHostPnicCDP for check CDP/LLDP information for ESXi hostsystem physical nic. He has really done good job,
After seeing his blog I remembered, Long back I had also written script to pull information CDP and LLDP from vCenter servers. I had  already posted script for only CDP - PowerCLI esxi host Physical nic info in nice table format 

Below script for pull both the information for CDP and LLDP, I tested it long back working. Copy script it test, save as extension .PS1, make sure your machine execution policy is no restricted. Change the IP to your vcenter or esxi server.

Other Useful Scripts:

Save complete virtual PortGroup information Settings - Powercli


 #####################################   
  ## http://kunaludapi.blogspot.com   
  ## Version: 1   
  ##   
  ## Tested this script on   
  ## 1) Powershell v3   
  ## 2) Powercli v5.5   
  ## 3) Vsphere 5.x   
  #####################################   
 Add-PSSnapin VMware.VimAutomation.core   
 Add-PSSnapin VMware.VimAutomation.Vds   
  $vCenterCred = Get-Credential -Message "vCenter server or esxi credentials"   
  $vcenterServer = "192.168.33.21"   
  Connect-viServer -server $vcenterServer -Credential $vCenterCred   
  $Collection = @()   
  $Esxihosts = Get-VMHost rginitesx003.rageprod.com ,rginitesxsrv15.rageprod.com #| Where-Object {$_.ConnectionState -eq "Connected"}   
  foreach ($Esxihost in $Esxihosts) {   
   $Esxcli = Get-EsxCli -VMHost $Esxihost   
   $Esxihostview = Get-VMHost $EsxiHost | Get-View   
   $NetworkSystem = $Esxihostview.Configmanager.Networksystem   
   $Networkview = Get-View $NetworkSystem   
   $DvSwitchInfo = Get-VDSwitch -VMHost $Esxihost   
   if ($DvSwitchInfo -ne $null) {   
      $DvSwitchHost = $DvSwitchInfo.ExtensionData.Config.Host   
      $DvSwitchHostView = Get-View $DvSwitchHost.config.host   
      $VMhostnic = $DvSwitchHostView.config.network.pnic   
      $DVNic = $DvSwitchHost.config.backing.PnicSpec.PnicDevice   
   }   
   $VMnics = $Esxihost | get-vmhostnetworkadapter -Physical  #$_.NetworkInfo.Pnic   
   Foreach ($VMnic in $VMnics){   
     $realInfo = $Networkview.QueryNetworkHint($VMnic)   
     $pNics = $esxcli.network.nic.list() | where-object {$vmnic.name -eq $_.name} | Select-Object Description, Link        
     $Description = $esxcli.network.nic.list()   
     if ($vmnic.Name -eq $DVNic) {   
       $vSwitch = $DVswitchInfo | where-object {$DVNic -match $vmnic.Name} | select-object -ExpandProperty Name   
     }   
     else {   
       $vSwitchname = $Esxihost | Get-VirtualSwitch | Where-object {$_.nic -eq $VMnic.DeviceName}   
       $vSwitch = $vSwitchname.name   
     }   
     if ($realInfo.lldpinfo -ne $null) {  
       $LLDPinfo = $realInfo.lldpinfo  
       $SwitchName = $realInfo.lldpinfo.Parameter | Where-Object {$_.Key -eq "System Name"} | Select-Object -ExpandProperty Value  
       $SwitchIP = $realInfo.lldpinfo.Parameter | Where-Object {$_.Key -eq "Management Address"} | Select-Object -ExpandProperty Value  
       $SwitchPortVlanID = $realInfo.lldpinfo.Parameter | Where-Object {$_.Key -eq "Vlan ID"} | Select-Object -ExpandProperty Value  
       $SwitchPortMTU = $realInfo.lldpinfo.Parameter | Where-Object {$_.Key -eq "MTU"} | Select-Object -ExpandProperty Value  
       $Table = New-Object PSObject   
       $Table | Add-Member -Name EsxName -Value $esxihost.Name -MemberType NoteProperty   
       $Table | Add-Member -Name VMNic -Value $VMnic -MemberType NoteProperty   
       $Table | Add-Member -Name vSwitch -Value $vSwitch -MemberType NoteProperty   
       $Table | Add-Member -Name Link -Value $pNics.Link -MemberType NoteProperty    
       $Table | Add-Member -Name PortNo -Value $LLDPinfo.PortId -MemberType NoteProperty   
       $Table | Add-Member -Name SwitchName -Value $SwitchName -MemberType NoteProperty   
       $Table | Add-Member -Name SwitchIP -Value $SwitchIP -MemberType NoteProperty  
       $Table | Add-Member -Name MacAddress -Value $vmnic.Mac -MemberType NoteProperty   
       $Table | Add-Member -Name SpeedMB -Value $vmnic.ExtensionData.LinkSpeed.SpeedMB -MemberType NoteProperty   
       $Table | Add-Member -Name Duplex -Value $vmnic.ExtensionData.LinkSpeed.Duplex -MemberType NoteProperty   
       $Table | Add-Member -Name Pnic-Vendor -Value $pNics.Description -MemberType NoteProperty   
       $Table | Add-Member -Name Pnic-drivers -Value $vmnic.ExtensionData.Driver -MemberType NoteProperty   
       $Table | Add-Member -Name PCI-Slot -Value $vmnic.ExtensionData.Pci -MemberType NoteProperty   
       $collection += $Table  
     }  
     else {  
       $CDPextended = $realInfo.connectedswitchport  
       $Table = New-Object PSObject   
       $Table | Add-Member -Name EsxName -Value $esxihost.Name -MemberType NoteProperty   
       $Table | Add-Member -Name VMNic -Value $VMnic -MemberType NoteProperty   
       $Table | Add-Member -Name vSwitch -Value $vSwitch -MemberType NoteProperty   
       $Table | Add-Member -Name Link -Value $pNics.Link -MemberType NoteProperty    
       $Table | Add-Member -Name PortNo -Value $CDPextended.PortId -MemberType NoteProperty   
       $Table | Add-Member -Name SwitchName -Value $CDPextended.devID -MemberType NoteProperty   
       $Table | Add-Member -Name SwitchIP -Value $CDPextended.Address -MemberType NoteProperty  
       $Table | Add-Member -Name MacAddress -Value $vmnic.Mac -MemberType NoteProperty   
       $Table | Add-Member -Name SpeedMB -Value $vmnic.ExtensionData.LinkSpeed.SpeedMB -MemberType NoteProperty   
       $Table | Add-Member -Name Duplex -Value $vmnic.ExtensionData.LinkSpeed.Duplex -MemberType NoteProperty   
       $Table | Add-Member -Name Pnic-Vendor -Value $pNics.Description -MemberType NoteProperty   
       $Table | Add-Member -Name Pnic-drivers -Value $vmnic.ExtensionData.Driver -MemberType NoteProperty   
       $Table | Add-Member -Name PCI-Slot -Value $vmnic.ExtensionData.Pci -MemberType NoteProperty  
       $collection += $Table   
     }  
   }   
  }   
  $Collection | Sort-Object esxname, vmnic | ft *   
  Disconnect-VIserver * -confirm:$false   

5 comments:

Anonymous said...

I came across this blog post and this script was very useful. I did find that it did not pull the distributed vswitch name. I altered the if statement to if ($DMNic.contains($vmnic.Name)) and it worked properly

Maximus said...

Where or what line did you replace?

Anonymous said...

Row 34:
if ($DVNic.contains($vmnic.Name)) {

Hygor C. Tavares said...

It helped a lot in a task I had to perform. But I made some attempts to export to CSV or EXCEL without success. Is it possible?

Anonymous said...

It's possible to include the datacenter and cluster?