PowerCLI – Get physical switch port information

During fork lift host migrations or physical switch replacement, being able to extract physical port and switch information is vital
I found this at 2 am one morning, I think I made some modifications, but it was a real life saver

 

[code language=”powershell”]
param($VMHost)
$vmh = Get-VMHost $VMHost
If ($vmh.ConnectionState -ne "Connected") {
Write-Output "Host $($vmh) state is not connected, skipping."
}
Else {
Get-View $vmh.ID | `
% { $esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} | `
% { foreach ($physnic in $_.NetworkInfo.Pnic) {
$pnicInfo = $_.QueryNetworkHint($physnic.Device)
foreach( $hint in $pnicInfo ){
# Write-Host $esxname $physnic.Device
if ( $hint.ConnectedSwitchPort ) {
$hint.ConnectedSwitchPort | select @{n="VMHost";e={$esxname}},@{n="VMNic";e={$physnic.Device}},DevId,Address,PortId,HardwarePlatform
}
else {
Write-Host "No CDP information available."
}
}
}
}
}
[/code]

 

run it against a single host

.\Get-VMHostCDPInfo.ps1 <esx host name>

It will output, vmnic, switch ip, switch port, switch hardware

PowerCLI C:\vSpherePowerCLI\Scripts> .\Get-VMHostCDPInfo.ps1 esx01.testlab.local


VMHost           : esx01.testlab.local
VMNic            : vmnic0
DevId            : swxcus1(SSIXXXXXxxx)
Address          : 192.32.71.163
PortId           : Ethernet100/1/19
HardwarePlatform : N5K-C5548UP

VMHost           : esx01.testlab.local
VMNic            : vmnic1
DevId            : swxcus1(SSIXXXXXxxx)
Address          : 192.168.71.163
PortId           : Ethernet101/1/19
HardwarePlatform : N5K-C5548UP

VMHost           : esx01.testlab.local
VMNic            : vmnic2
DevId            : swxbck1(SSIXXXXXxxx)
Address          : 192.168.71.238
PortId           : Ethernet101/1/16
HardwarePlatform : N5K-C5548UP

VMHost           : esx01.testlab.local
VMNic            : vmnic3
DevId            : swxbck1(SSIXXXXXxxx)
Address          : 192.168.71.238
PortId           : Ethernet100/1/16
HardwarePlatform : N5K-C5548UP

VMHost           : esx01.testlab.local
VMNic            : vmnic4
DevId            : sxpro1(SSIXXXXXxxx)
Address          : 192.168.65.211
PortId           : Ethernet101/1/19
HardwarePlatform : N5K-C5548UP

VMHost           : esx01.testlab.local
VMNic            : vmnic5
DevId            : sxpro2(SSIXXXXXxxx)
Address          : 192.168.65.212
PortId           : Ethernet101/1/19
HardwarePlatform : N5K-C5548UP

Leave a comment

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.