$MyOutput = @()
foreach($VMHost in (Get-View -ViewType HostSystem)){
$netSys = Get-View $VMHost.ConfigManager.NetworkSystem
$vswTab = @{}
foreach($vsw in $netSys.NetworkInfo.vSwitch){
foreach($pnic in $vsw.Pnic){
$vswTab.Add(($netSys.NetworkInfo.Pnic | where {$_.Key -eq $Pnic}).Device,$vsw.Name)
}
}
foreach ($physnic in $netSys.NetworkInfo.Pnic) {
$pnicInfo = $netSys.QueryNetworkHint($physnic.Device)
foreach($hint in $pnicInfo){
$Report = "" | Select VMHost,vSwitch,vmnic,Status,MAC,Speed,Vlan,PortId,DeviceId,Observed
$Report.VMHost = $VMHost.Name
$Report.vmnic = $physnic.Device
$Report.vSwitch = $vswTab[$physnic.Device]
$Report.DeviceId = $hint.connectedSwitchPort.DevId
$Report.PortId = $hint.connectedSwitchPort.PortId
$Report.Vlan = $hint.connectedSwitchPort.Vlan
$record = 0
Do{
If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
$Report.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
$Report.MAC = $vmhost.Config.Network.Pnic[$record].Mac
}
$record ++
}
Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
foreach ($obs in $Hint.Subnet){
$Report.Observed += $obs.IpSubnet + " "
}
If (($Report.Speed -ge 1000) -and ($Hint.ConnectedSwitchPort.FullDuplex -eq $true)){
$Report.Status = "OK"
}Else{If (($Report.Speed -gt 0) -and ($Hint.ConnectedSwitchPort.FullDuplex -eq "")){
$Report.Status = "CDP not working"
}ElseIf (($Report.Speed -gt 0) -and ($Hint.ConnectedSwitchPort.FullDuplex -eq $false)){
if ($Status -ne ""){$Status += " / "}
$Report.Status = "Duplex config error"
}ElseIf (($Report.Speed -gt 0) -and ($Report.Speed -lt 1000)){
if ($Report.Status -ne ""){$Report.Status += " / "}
$Report.Status = "Speed config error"
}Else{
$Report.Status = "Link Down"
}
}
$MyOutput += $Report
}
}
$MyOutput | Sort VMHost,vSwitch,vmnic | Export-Csv -Path C:\NetworkAudit.csv -NoTypeInformation
}
Let us know how it goes.