VMware Cloud Community
faherne_CTI
Enthusiast
Enthusiast

Collect vDS Portgroup, VMNIC and CDP information

Hi,

I found an awesome PowerCLI function that works fantastically, but I'd really like to get a slightly modified table output that included the following:

| VMHost | PortGroup Name | VMNIC ID | VLAN No. | Switch Port ID |  Switch Port ID Connected |  Switch Name | Switch VLAN IP |

The following article gives me almost all of the above except:

  • The vDS Portgroup Name
  • The vDS PortGroup VLAN number
  • Take into account that a VMNIC may be a member of more than 1 portgroup

https://www.jonathanmedd.net/2013/07/obtaining-cdp-info-via-powercli.html

I did find a different PowerCLI script that pulls the vDS portgroup VLAN, but doesn't include the configured PortGroup Name, and doesn't take into account that a VMNIC may be a member of more than one PortGroup: Cisco Discovery Protocol

Here's my slightly modified code, but I do not know how to integrate the 3 bullet points above:

####################################################################################

function Get-VMHostNetworkAdapterCDP {
<#
.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 = @{
MangementAddress = $PhysicalNicHintInfo.ConnectedSwitchPort.MgmtAddr
Switch = $PhysicalNicHintInfo.ConnectedSwitchPort.DevId
Connected = $Connected
PortId = $PhysicalNicHintInfo.ConnectedSwitchPort.PortId
NIC = $PNIC.Device
VMHost = $ESXiHost.Name
#HardwarePlatform = $PhysicalNicHintInfo.ConnectedSwitchPort.HardwarePlatform
#SoftwareVersion = $PhysicalNicHintInfo.ConnectedSwitchPort.SoftwareVersion
}
$Object = New-Object PSObject -Property $hash
$CDPObject += $Object
}
}
}
catch [Exception] {

throw "Unable to retrieve CDP info"
}
}
end {

Write-Output $CDPObject
}
}

$MyCluster = "<Insert Clustername>"

Get-Cluster $MyCluster| Get-VMHost | Get-VMHostNetworkAdapterCDP | Select VMHost,NIC,PortId,Connected,Switch,MangementAddress | Export-Csv .\$($MyCluster)"_CDP_Info.csv" -NoTypeInformation -UseCulture

##################################################################################################

Reply
0 Kudos
21 Replies
LucD
Leadership
Leadership

No, this snippet is only for all pNICs connected to VDS.
A full script, that handles all pNICs, unused/VSS/VDS, would require combining the 2 snippets in this thread together with some code to handle the unused pNICs.

If you mean the CDP DevId value, I added that in the code above.
In fact, you can add all CDP info in the report.


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

likeahoss
Enthusiast
Enthusiast

What IDE do you use?

Reply
0 Kudos