Last active
November 12, 2023 14:24
-
-
Save phyoewaipaing/506ef90df6ade844151dbd2d9776ddfe to your computer and use it in GitHub Desktop.
Get CDP info of multiple Esxi Hosts
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
## Get CDP info of single/multiple Esxi hosts ## | |
## It's based on the original work of Jonathan Medd's script (https://www.jonathanmedd.net/2013/07/obtaining-cdp-info-via-powercli.html), I just add the Get-VMhost and Export-Csv option to find all the CDP info of the connected esxi hosts ## | |
## Author : Phyoe Wai Paing | |
## Country: Myanmar(Burma) | |
## Changed History : | |
## Version : 22.May.2016 : 1.0 : Initial release | |
## : 12.Apr.2022 : 1.1 : Add the validation of hostlist.txt file and export to csv by default in addition to displaying the result in the console | |
function Get-VMHostNetworkAdapterCDP { | |
<# | |
********************************************************************** | |
***** The function will exist if there is 1 disconnected Host ******** | |
********************************************************************** | |
.SYNOPSIS | |
Function to retrieve the Network Adapter CDP info of a vSphere host. | |
.DESCRIPTION | |
Function to retrieve the Network Adapter CDP info of a vSphere host. | |
.PARAMETER VMHost | |
A vSphere ESXi Host object | |
.INPUTS | |
System.Management.Automation.PSObject. | |
.OUTPUTS | |
System.Management.Automation.PSObject. | |
.EXAMPLE | |
PS> Get-VMHostNetworkAdapterCDP -VMHost ESXi01,ESXi02 | |
.EXAMPLE | |
PS> Get-VMHost ESXi01,ESXi02 | Get-VMHostNetworkAdapterCDP | |
#> | |
[CmdletBinding()][OutputType('System.Management.Automation.PSObject')] | |
Param | |
( | |
[parameter(Mandatory=$true,ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[PSObject[]]$VMHost | |
) | |
begin { | |
$ErrorActionPreference = 'Stop' | |
Write-Debug $MyInvocation.MyCommand.Name | |
$CDPObject = @() | |
} | |
process{ | |
try { | |
foreach ($ESXiHost in $VMHost){ | |
if ($ESXiHost.GetType().Name -eq "string"){ | |
try { | |
$ESXiHost = Get-VMHost $ESXiHost -ErrorAction Stop | |
} | |
catch [Exception]{ | |
Write-Warning "VMHost $ESXiHost does not exist" | |
} | |
} | |
elseif ($ESXiHost -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]){ | |
Write-Warning "You did not pass a string or a VMHost object" | |
Return | |
} | |
$ConfigManagerView = Get-View $ESXiHost.ExtensionData.ConfigManager.NetworkSystem | |
$PNICs = $ConfigManagerView.NetworkInfo.Pnic | |
foreach ($PNIC in $PNICs){ | |
$PhysicalNicHintInfo = $ConfigManagerView.QueryNetworkHint($PNIC.Device) | |
if ($PhysicalNicHintInfo.ConnectedSwitchPort){ | |
$Connected = $true | |
} | |
else { | |
$Connected = $false | |
} | |
$hash = @{ | |
VMHost = $ESXiHost.Name | |
NIC = $PNIC.Device | |
Connected = $Connected | |
Switch = $PhysicalNicHintInfo.ConnectedSwitchPort.DevId | |
HardwarePlatform = $PhysicalNicHintInfo.ConnectedSwitchPort.HardwarePlatform | |
SoftwareVersion = $PhysicalNicHintInfo.ConnectedSwitchPort.SoftwareVersion | |
MangementAddress = $PhysicalNicHintInfo.ConnectedSwitchPort.MgmtAddr | |
PortId = $PhysicalNicHintInfo.ConnectedSwitchPort.PortId | |
VlanID=$PhysicalNicHintInfo.ConnectedSwitchPort.Vlan | |
} | |
$Object = New-Object PSObject -Property $hash | |
$CDPObject += $Object | |
} | |
} | |
} | |
catch [Exception] { | |
throw "Unable to retrieve CDP info" | |
} | |
} | |
end { | |
$CDPObject | where { $_.Connected -eq $True } | ft VMHost,NIC,VlanID,Switch,Connected,PortId,MangementAddress,HardwarePlatform,SoftwareVersion -auto | |
# Uncomment the following line if you want to export to CSV sheet | |
$CDPObject | where { $_.Connected -eq $True } | select VMHost,NIC,VlanID,Switch,Connected,PortId,MangementAddress,HardwarePlatform,SoftwareVersion | export-csv .\CDP_Networkinfo.csv -notypeinfo | |
Write-Host "CDP Info exported to CDP_Networkinfo.csv" | |
} | |
} | |
If ((Test-Path -Path .\hostlist.txt) -AND !([string]::IsNullOrWhiteSpace((Get-Content -Path .\hostlist.txt)))) | |
{ | |
$hostlist= Get-Content hostlist.txt | |
Write-Host "Getting the CDP Info for $($hostlist.Count) hosts. Please wait..." | |
$hostlist | Get-VMHostNetworkAdapterCDP | |
} | |
else | |
{ | |
$hostlist = Get-VMHost | ? { $_.ConnectionState -eq "Connected" } | |
Write-Host "Getting the CDP Info for $($hostlist.Count) hosts. Please wait..." | |
$hostlist | Get-VMHostNetworkAdapterCDP | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment