PoshCode Logo PowerShell Code Repository

VMware Host Network Info by alanrenouf 3 years ago
View followups from alanrenouf | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/855"></script>download | new post

The following script will add some nice host network information into an object which is exported to a csv file for passing to the network guys or can be used to find your server in that mess of cables that are always meaning to be tidied in the data center.

You will get:
Host,
Physical Nic Name
Speed
MAC
Switch Device ID
Port ID
Observed Network ranges
VLAN’s

  1. Connect-VIServer MYVISERVER
  2. $filename = "c:\DetailedNetworkInfo.csv"
  3. Write "Gathering VMHost objects"
  4. $vmhosts = Get-VMHost | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View
  5. $MyCol = @()
  6. foreach ($vmhost in $vmhosts){
  7.  $ESXHost = $vmhost.Name
  8.  Write "Collating information for $ESXHost"
  9.  $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
  10.  foreach($pnic in $networkSystem.NetworkConfig.Pnic){
  11.      $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)
  12.      foreach($Hint in $pnicInfo){
  13.          $NetworkInfo = "" | select-Object Host, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN
  14.          $NetworkInfo.Host = $vmhost.Name
  15.          $NetworkInfo.PNic = $Hint.Device
  16.          $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId
  17.          $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
  18.          $record = 0
  19.          Do{
  20.              If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
  21.                  $NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
  22.                  $NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac
  23.              }
  24.              $record ++
  25.          }
  26.          Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
  27.          foreach ($obs in $Hint.Subnet){
  28.              $NetworkInfo.Observed += $obs.IpSubnet + " "
  29.              Foreach ($VLAN in $obs.VlanId){
  30.                  If ($VLAN -eq $null){
  31.                  }
  32.                  Else{
  33.                      $strVLAN = $VLAN.ToString()
  34.                      $NetworkInfo.VLAN += $strVLAN + " "
  35.                  }
  36.              }
  37.          }
  38.          $MyCol += $NetworkInfo
  39.      }
  40.  }
  41. }
  42. $Mycol | Sort Host, PNic | Export-Csv $filename -NoTypeInformation

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me