VMware PowerCLI – Report Cisco Discovery Protocol Details

For those that have more than 10 physical NICs on your hosts you may find existing CDP scripts lack a little when trying to sort by hostname and then NIC

vmnic0
vmnic1
vmnic11
vmnic12
vmnic2
.
.
vmnicN

Nothing like a little regex and built-in padding to get everything in order.  Pass a vmhost host object to this function to get a report on all physical NICs and including padding for vmnic[0-9] matches so they are returned as vmnic0[0-9]:

function Get-VMHostNetworkAdapterCDP
{
  [CmdletBinding()]
  Param(
   [Parameter(Mandatory=$true,Position=0)]
   [ValidateScript({Get-View $_.ExtensionData.ConfigManager.NetworkSystem})]
   $VMhost
  )

  $view = Get-View $VMhost.ExtensionData.ConfigManager.NetworkSystem
  $physicalNICarr = $view.NetworkConfig.Pnic
  $report = @()
  foreach($nic in $physicalNICarr)
  {
    $nicHint = $view.QueryNetworkHint($nic.Device)
   
    $props = @{
      VMHost = $VMhost.Name
      NIC = ''
      Connected = if($nicHint.ConnectedSwitchPort){$true} else {$false}
      Switch = $nicHint.ConnectedSwitchPort.DevId
      PortId = $nicHint.ConnectedSwitchPort.PortId      
      HardwarePlatform = $nicHint.ConnectedSwitchPort.HardwarePlatform
      SoftwareVersion = $nicHint.ConnectedSwitchPort.SoftwareVersion
      MangementAddress = $nicHint.ConnectedSwitchPort.MgmtAddr
    }

    switch ($nic.Device.length)
    { 
      6       {$nic.Device -match '(\w+)(\d)' | Out-Null; $props.NIC = $matches[1] + $matches[2].PadLeft(2,'0')}
      default {$props.NIC = $nic.Device}
    }  

    $report += New-Object PSObject -Property $props
  }
  $report
}