More Network Info

Following my previous network information script I was asked to add the VSwitch, so here it is…

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
  • VSwitch
  • VSwitch Ports (Added for @gabvirtualworld)
  • VSwitch Ports in use (Added for @gabvirtualworld)
  • Physical Nic Name
  • Speed
  • MAC
  • Switch Device ID
  • Port ID
  • Observed Network ranges
  • VLAN’s

Missing anything ?!
Sample output:
SampleNetworkInfo

# Set the VI Server and Filename before running
Connect-VIServer MYVISERVER
$filename = "C:\DetailedNetworkInfo.csv"

Write "Gathering VMHost objects"
$vmhosts = Get-VMHost | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View
$MyCol = @()
foreach ($vmhost in $vmhosts){
 $ESXHost = $vmhost.Name
 Write "Collating information for $ESXHost"
 $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
 foreach($pnic in $networkSystem.NetworkConfig.Pnic){
     $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)
     foreach($Hint in $pnicInfo){
         $NetworkInfo = "" | select-Object Host, vSwitch, vSwitchPorts, vSwitchPrtInUse, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN
         $NetworkInfo.Host = $vmhost.Name
		 $NetworkInfo.vSwitch = Get-Virtualswitch -VMHost (Get-VMHost ($vmhost.Name)) | where {$_.Nic -eq ($Hint.Device)}
         $NetworkInfo.vSwitchPorts = $NetworkInfo.vSwitch.NumPorts
		 $NetworkInfo.vSwitchPrtInUse = ($NetworkInfo.vSwitch.NumPorts - $NetworkInfo.vSwitch.NumPortsAvailable)
		 $NetworkInfo.PNic = $Hint.Device
         $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId
         $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
         $record = 0
         Do{
             If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
                 $NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
                 $NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac
             }
             $record ++
         }
         Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
         foreach ($obs in $Hint.Subnet){
             $NetworkInfo.Observed += $obs.IpSubnet + " "
             Foreach ($VLAN in $obs.VlanId){
                 If ($VLAN -eq $null){
                 }
                 Else{
                     $strVLAN = $VLAN.ToString()
                     $NetworkInfo.VLAN += $strVLAN + " "
                 }
             }
         }
         $MyCol += $NetworkInfo
     }
 }
}
$Mycol | Sort Host, PNic | Export-Csv $filename -NoTypeInformation

27 thoughts on “More Network Info

  1. Mohamed

    I am unable to find where this script – network information is.. can any one help me out?

  2. Scott D

    I am trying to capture vDS Monitoring Port State information to report on traffic usage. Where am I missing this information? I would like to capture and display the current Unicast – ingress and egress trafficvalues.

  3. Jim Greeley

    I’m new to using scripting, so these may sound like dumb questions. How do I execute this script? Do I put the name changes for the hosts in first or does the script prompt for the server names?

  4. Caleb

    I recently sent a reply about running this script on a standalone host. I have an edit: In case the variable has the FDQN you will need to first strip the FDQN of the domain then add the wild card. Then the script will run in all senarios of standalone and vcenter.

    $NetworkInfo.vSwitch = Get-Virtualswitch -VMHost (Get-VMHost ($vmhost.Name.Split(“.”)[0] + “*”)) | where {$_.Nic -eq ($Hint.Device)}

  5. caleb

    I was having issues running the script against a standalone ESXi host. The problem was with the name of the ESXi host in the following line. I added the wild card to make it work. Hope this helps. Thanks for composing such a useful script.

    ORG:
    $NetworkInfo.vSwitch = Get-Virtualswitch -VMHost (Get-VMHost ($vmhost.Name)) | where {$_.Nic -eq ($Hint.Device)}

    NEW:
    $NetworkInfo.vSwitch = Get-VDSwitch -VMHost (Get-VMHost ($vmhost.Name + “*”)) | where {$_.Nic -eq ($PNic.Device)}

  6. Caleb

    Any chance that you could update this script to use the get-vdswitch cdmlet? I get an error stating that the get-virtualswitch cmdlet is depreciated.

  7. Guillermo

    Hi Alan, thanks for this script… Is it possible to get the physical adapter name/model to see virtual switches are configured across different adapters for HA? In my case I have 4 NICs per adapter Regards

  8. Wes

    Has anyone been able to get Duplex, port group and full VLAN info into this? The VLAN part of this only gets the first VLAN it sees.

    Thanks

  9. Fred Witteman

    I have 2 ntp servers:
    10.10.10.80
    and
    10.10.10.85
    How should i define the $ntpserver ?

  10. Pingback: Script: Detailed NIC Information Report « Micronauts

  11. Matt

    great script!
    Is it possible to add the load balancing setting for each vswitch? Ip_hash, src_id etc? I’ve been trying to do this myself but not having much luck so far.

  12. Pingback: PowerCLI: Host Network Config « Virtu-Al

  13. Ken Cline

    Something that would be useful to add, in addition to number of vSwitch ports, is the number of vSwitch ports that are still available. While this hasn’t been much of a problem since the ESX 2.x days (when there were only 32 ports per vSwitch – period) – I can certainly see it becoming more of an issue as people are beginning to stack up 50, 60, or more VMs per host…

  14. Virtu-Al

    Iben, Thanks for your kind words, you have some great Ideas there, something for me to tackle when I return from VMworld.

    I like the idea of turning it into a tool, might even add a GUI, I have been looking for an excuse to try PrimalForms.

    The other additions are great, they will definately be added.

  15. Iben Rodriguez

    Well, since you’re asking for feedback – it would be great to see the load balancing method (# “Route based on originating virtual port ID”, “Route based on source MAC hash”, “Use explicit failover order”, or “Route based on ip hash”) and link failure detection method (Beacon or Link).

    This should be listed for each vSwitch and the port groups should list if they use the vSwitch settings or if it is being overridden.

    Also for the vSwitch and port groups if each vmnic is active or standby.

    I wonder if we can use this to make a tool like RANCID http://en.wikipedia.org/wiki/RANCID?

    I like your blog. Keep up the great work. very useful. Thanks!

    I b e n

  16. forbes

    Hi, I just noticed something very interesting. This script actually gives the “observed” VLANs, not just the port group VLAN IDs you set on the vSwitches. This is something that you can’t get from your VI client (it only provides observed IP ranges). Looking at the code, I assume that it doesn’t get this via CDP, but from listening to the traffic. Obviously you have to be careful with this type of data, however I think this is a real bonus to the tool. Very useful for troubleshooting network issues! Thanks

  17. Virtu-Al

    forbes, thats fine, let me know if you work it out. Thanks for taking the time to read my blog !

  18. forbes

    OK, very weird. I just ran it again and its fine. Please ignore this (and delete the post if you like), while I try to figure out what was different).

  19. Virtu-Al

    That’s strange, as you can see from my sample output there are hosts with nics not attached to VSwitches and it continues on.

  20. forbes

    There seems to be a bit of a bug. When I run the script it runs fine until it gets to host, where one of the vmnics is not assigned to a vSwitch. From that point on it just duplicates the details of the last port down the spreadsheet.
    Try removing a vmnic from one of your hosts and run it again.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.