Src/Private/Get-VMHostNetworkAdapterCDP.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
    Get-VMHostNetworkAdapterCDP -VMHost ESXi01,ESXi02
    .EXAMPLE
    Get-VMHost ESXi01,ESXi02 | Get-VMHostNetworkAdapterCDP
    #>

    [CmdletBinding()][OutputType('System.Management.Automation.PSObject')]

    Param
    (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [PSObject[]]$VMHosts   
    )    

    begin {
        $CDPObject = @()
    }

    process {
        try {
            foreach ($VMHost in $VMHosts) {
                $ConfigManagerView = Get-View $VMHost.ExtensionData.ConfigManager.NetworkSystem
                $pNics = $ConfigManagerView.NetworkInfo.Pnic
                foreach ($pNic in $pNics) {
                    $PhysicalNicHintInfo = $ConfigManagerView.QueryNetworkHint($pNic.Device)
                    $Object = [PSCustomObject]@{                            
                        'Host' = $VMHost.Name
                        'Device' = $pNic.Device
                        'Status' = if ($PhysicalNicHintInfo.ConnectedSwitchPort) {
                            'Connected'
                        } else {
                            'Disconnected'
                        }
                        'SwitchId' = $PhysicalNicHintInfo.ConnectedSwitchPort.DevId
                        'Address' = $PhysicalNicHintInfo.ConnectedSwitchPort.Address
                        'VLAN' = $PhysicalNicHintInfo.ConnectedSwitchPort.Vlan
                        'MTU' = $PhysicalNicHintInfo.ConnectedSwitchPort.Mtu
                        'SystemName' = $PhysicalNicHintInfo.ConnectedSwitchPort.SystemName
                        'Location' = $PhysicalNicHintInfo.ConnectedSwitchPort.Location
                        'HardwarePlatform' = $PhysicalNicHintInfo.ConnectedSwitchPort.HardwarePlatform
                        'SoftwareVersion' = $PhysicalNicHintInfo.ConnectedSwitchPort.SoftwareVersion
                        'ManagementAddress' = $PhysicalNicHintInfo.ConnectedSwitchPort.MgmtAddr
                        'PortId' = $PhysicalNicHintInfo.ConnectedSwitchPort.PortId
                    }
                    $CDPObject += $Object
                }
            }
        } catch [Exception] {
            throw 'Unable to retrieve CDP info'
        }
    }
    end {
        Write-Output $CDPObject
    }
}