alanrenouf
VMware Employee
VMware Employee

Detailed Host Netwok Information

Hi,

After someone asking me whether I could get them some specific network info for each host I have created a script for getting detailed host network information and was just wondering if I was missing any vital information (apart from comments ofcourse Smiley Wink) before I posted it on my blog, give it a go and let me know what you think.... Thanks

Connect-VIServer MYSERVER
$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, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN
			$NetworkInfo.Host = $vmhost.Name
			$NetworkInfo.PNic = $Hint.Device 
			$NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId 
			$NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
			$NetworkInfo.VLAN = $Hint.connectedSwitchPort.Vlan
			$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 + " "
			}
			$MyCol += $NetworkInfo
		}
	}
}
$Mycol | Sort Host, PNic | Export-Csv $filename -NoTypeInformation

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
4 Replies
LucD
Leadership
Leadership

Alan, great script.

One remark, the deviceId and portId can only be retrieved when the pNIC is connected to a CDP-aware network device.

The VLAN-id can also be obtained via the PhysicalNicIpHint object (that would be $obs.VlanId in your script).

So you could report that even if there are no CDP-aware devices.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

alanrenouf
VMware Employee
VMware Employee

Thanks thats a great idea, I was hoping you would add your comments Smiley Wink

I hope VMware see sence and make sure you get a V V P, I will be voting for you !

Alan

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
juststormY
Contributor
Contributor

Hello together,

short question on an ending friday:

is it possible to add also the virtual switch of every pnic and post this also in the csv file into a new line?

I don't think its a big problem, is it?

Thanks for info!

0 Kudos
alanrenouf
VMware Employee
VMware Employee

Just incase you didnt see the other post...


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, 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.PNic = $Hint.Device 
            $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId 
            $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId
            $NetworkInfo.VLAN = $Hint.connectedSwitchPort.Vlan
            $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 + " "
            }
            $MyCol += $NetworkInfo
        }
    }
}
$Mycol | Sort Host, PNic | Export-Csv $filename -NoTypeInformation
 


If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

VMware, Citrix, Microsoft Consultant

UK

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos