PoshCode Logo PowerShell Code Repository

VMware Host Network Info by alanrenouf 36 months ago (modification of post by alanrenouf view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/872"></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, Num of VSwitch Ports and Num of VSwitch ports in use

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, vSwitchPorts, vSwitchPrtInUse, 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.vSwitchPorts = $NetworkInfo.vSwitch.NumPorts
  19.                  $NetworkInfo.vSwitchPrtInUse = ($NetworkInfo.vSwitch.NumPorts - $NetworkInfo.vSwitch.NumPortsAvailable)
  20.                  $NetworkInfo.PNic = $Hint.Device
  21.          $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId
  22.          $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
  23.          $record = 0
  24.          Do{
  25.              If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
  26.                  $NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
  27.                  $NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac
  28.              }
  29.              $record ++
  30.          }
  31.          Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
  32.          foreach ($obs in $Hint.Subnet){
  33.              $NetworkInfo.Observed += $obs.IpSubnet + " "
  34.              Foreach ($VLAN in $obs.VlanId){
  35.                  If ($VLAN -eq $null){
  36.                  }
  37.                  Else{
  38.                      $strVLAN = $VLAN.ToString()
  39.                      $NetworkInfo.VLAN += $strVLAN + " "
  40.                  }
  41.              }
  42.          }
  43.          $MyCol += $NetworkInfo
  44.      }
  45.  }
  46. }
  47. $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