Last active
May 14, 2018 19:00
-
-
Save Zsoldier/20439375b0781a799c5141e2413d0842 to your computer and use it in GitHub Desktop.
Get-CDP or LLDP Info from ESXi/vCenter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-CDPorLLDP { | |
$myCol = @() | |
foreach ($VIServer in $global:DefaultVIServers) | |
{ | |
$vmhosts = Get-VMHost -Server $VIServer | where-object {$_.ConnectionState -eq "Connected" -or "Maintenance"} | |
foreach ($vmhost in $vmhosts) | |
{ | |
Write-Host "Collating information for $($VMHost.Name)" | |
$networkSystem = Get-view -Server $viserver -Id $vmhost.extensiondata.ConfigManager.NetworkSystem | |
foreach($pnic in $networkSystem.NetworkConfig.Pnic) | |
{ | |
$pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device) | |
foreach($Hint in $pnicInfo) | |
{ | |
If (($vmhost.extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).LinkSpeed.SpeedMb -ne $null) | |
{ | |
$VMhostObject = $VMHost | |
$NetworkInfo = "" | select-Object vCenter, DataCenter, Host, Cluster, vSwitch, PNic, Speed, MAC, DeviceID, MgmtAddress, PortID, VLAN, NativeVLAN | |
$NetworkInfo.vCenter = $VIServer.Name | |
$NetworkInfo.DataCenter = (Get-Datacenter -VMHost $VMhostObject).Name | |
$NetworkInfo.Host = $vmhost.Name | |
#$NetworkInfo.Model = $vmhost.Hardware.SystemInfo.Model | |
$NetworkInfo.Cluster = $vmhostObject.Parent.name | |
$NetworkInfo.vSwitch = $null | |
$NetworkInfo.vSwitch = (Get-Virtualswitch -Server $VIServer -VMHost $VMhostObject -Standard | where-object {$_.Nic -eq ($Hint.Device)}).name | |
If ($NetworkInfo.vSwitch -eq $null) | |
{ | |
$NetworkInfo.vSwitch = (Get-VDSwitch -Server $VIServer -VMHost $VMhostObject | Get-VDPort -Uplink | where-object {$_.ConnectedEntity -match $Hint.Device -and $_.ProxyHost -match $vmhost.Name}).Switch.Name | |
If ($NetworkInfo.vSwitch -eq $null){$NetworkInfo.vSwitch = "Not Found"} | |
} | |
#Returns LLDP Info if CDP not found | |
If ($Hint.ConnectedSwitchPort -eq $null -and $Hint.LldpInfo -ne $null) | |
{ | |
$NetworkInfo.PNic = $Hint.Device | |
$NetworkInfo.DeviceID = ($Hint.LldpInfo.Parameter | where-object {$_.Key -match "System Name"}).Value | |
$NetworkInfo.MgmtAddress = ($Hint.LldpInfo.Parameter | where-object {$_.Key -match "Management Address"}).Value | |
$NetworkInfo.PortID = $Hint.LLDPInfo.PortId | |
$NetworkInfo.VLAN = If ($Hint.Subnet.Count -gt 1){"Trunk"} Elseif ($Hint.Subnet -eq $null) {"VLAN ID Hint not discovered yet"} Else {$Hint.Subnet.vlanid} | |
$NetworkInfo.NativeVLAN = $Hint.LLDPInfo.VLAN | |
$NetworkInfo.Speed = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).LinkSpeed.SpeedMb | |
$NetworkInfo.MAC = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).Mac | |
} | |
#Returns CDP Info if LLDP not found | |
If ($Hint.ConnectedSwitchPort -ne $null -and $Hint.LldpInfo -eq $null ) | |
{ | |
$NetworkInfo.PNic = $Hint.Device | |
$NetworkInfo.DeviceID = $Hint.ConnectedSwitchport.DevId | |
$NetworkInfo.MgmtAddress = $Hint.ConnectedSwitchport.Address | |
$NetworkInfo.PortID = $Hint.ConnectedSwitchPort.PortId | |
$NetworkInfo.VLAN = If ($Hint.Subnet.Count -gt 1){"Trunk"} Elseif ($Hint.Subnet -eq $null) {"VLAN ID Hint not discovered yet"} Else {$Hint.Subnet.vlanid} | |
$NetworkInfo.NativeVLAN = $Hint.ConnectedSwitchPort.VLAN | |
$NetworkInfo.Speed = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).LinkSpeed.SpeedMb | |
$NetworkInfo.MAC = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).Mac | |
} | |
#Returns info not found in case CDP and LLDP info not found | |
If ($Hint.ConnectedSwitchPort -eq $null -and $Hint.LldpInfo -eq $null ) | |
{ | |
$NetworkInfo.PNic = $Hint.Device | |
$NetworkInfo.DeviceID = "LLDP/CDP Info Not Found" | |
$NetworkInfo.MgmtAddress ="LLDP/CDP Info Not Found" | |
$NetworkInfo.PortID = "LLDP/CDP Info Not Found" | |
$NetworkInfo.VLAN = If ($Hint.Subnet.Count -gt 1){"Trunk"} Elseif ($Hint.Subnet -eq $null) {"VLAN ID Hint not discovered yet"} Else {$Hint.Subnet.vlanid} | |
$NetworkInfo.NativeVLAN = "LLDP/CDP Info Not Found" | |
$NetworkInfo.Speed = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).LinkSpeed.SpeedMb | |
$NetworkInfo.MAC = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).Mac | |
} | |
#Extremely unlikely scenario where both CDP and LLDP info found. | |
If ($Hint.ConnectedSwitchPort -ne $null -and $Hint.LldpInfo -ne $null ) | |
{ | |
$NetworkInfo.PNic = $Hint.Device | |
$NetworkInfo.DeviceID = "Ugh, that's weird. CDP AND LLDP info was found." | |
$NetworkInfo.MgmtAddress = "Ugh, that's weird. CDP AND LLDP info was found." | |
$NetworkInfo.PortID = "Ugh, that's weird. CDP AND LLDP info was found." | |
$NetworkInfo.VLAN = If ($Hint.Subnet.Count -gt 1){"Trunk"} Elseif ($Hint.Subnet -eq $null) {"VLAN ID Hint not discovered yet"} Else {$Hint.Subnet.vlanid} | |
$NetworkInfo.Speed = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).LinkSpeed.SpeedMb | |
$NetworkInfo.MAC = ($vmhost.Extensiondata.Config.Network.Pnic | where-object {$_.device -eq $hint.Device}).Mac | |
} | |
If ($NetworkInfo) {$MyCol += $NetworkInfo} | |
} | |
} | |
} | |
} | |
} | |
$myCol | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment