PoshCode Logo PowerShell Code Repository

VMware Host Network Info by alanrenouf 36 months ago (modification of post by alanrenouf view diff)
View followups from alanrenouf | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/870"></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. – Added VSwitch

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

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