Src/Private/Get-VMHostNetworkAdapterDP.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function Get-VMHostNetworkAdapterDP {
    <#
    .SYNOPSIS
    Function to retrieve the Network Adapter CDP or LLDP info of a vSphere host.
    .DESCRIPTION
    Function to retrieve the Network Adapter CDP or LLDP info of a vSphere host.
    .PARAMETER VMHost
    A vSphere ESXi Host object
    .INPUTS
    System.Management.Automation.PSObject.
    .OUTPUTS
    System.Management.Automation.PSObject.
    .EXAMPLE
    Get-VMHostNetworkAdapterDP -VMHost ESXi01,ESXi02
    .EXAMPLE
    Get-VMHost ESXi01,ESXi02 | Get-VMHostNetworkAdapterDP
    #>

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

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

    begin {
        $ObjOutput = @()
    }

    process {
        try {
            foreach ($ObjVMHost in $VMHost) {
                $ConfigManagerView = Get-View $ObjVMHost.ExtensionData.ConfigManager.NetworkSystem
                $pNics = $ConfigManagerView.NetworkInfo.Pnic
                foreach ($pNic in $pNics) {
                    $PhysicalNicHintInfo = $ConfigManagerView.QueryNetworkHint($pNic.Device)
                    if ($PhysicalNicHintInfo.ConnectedSwitchPort) {
                        $Object = [PSCustomObject]@{
                            'Host' = $ObjVMHost.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
                        }
                        $ObjOutput += $Object
                    }
                    if ($PhysicalNicHintInfo.LldpInfo) {
                        $Object = [PSCustomObject]@{
                            'Host' = $ObjVMHost.Name
                            'Device' = $pNic.Device
                            'ChassisId' = $PhysicalNicHintInfo.LldpInfo.ChassisId
                            'PortId' = $PhysicalNicHintInfo.LldpInfo.PortId
                            'TimeToLive' = $PhysicalNicHintInfo.LldpInfo.TimeToLive
                            'TimeOut' = ($PhysicalNicHintInfo.LldpInfo.Parameter | Where-Object {$_.key -eq "TimeOut"}).Value
                            'Samples' = ($PhysicalNicHintInfo.LldpInfo.Parameter | Where-Object {$_.key -eq "Samples"}).Value
                            'ManagementAddress' = ($PhysicalNicHintInfo.LldpInfo.Parameter | Where-Object {$_.key -eq "Management Address"}).Value
                            'PortDescription' = ($PhysicalNicHintInfo.LldpInfo.Parameter | Where-Object {$_.key -eq "Port Description"}).Value
                            'SystemDescription' = ($PhysicalNicHintInfo.LldpInfo.Parameter | Where-Object {$_.key -eq "System Description"}).Value
                            'SystemName' = ($PhysicalNicHintInfo.LldpInfo.Parameter | Where-Object {$_.key -eq "System Name"}).Value
                        }
                        $ObjOutput += $Object
                    }
                }
            }
        } catch [Exception] {
            throw 'Unable to retrieve CDP/LLDP info'
        }
    }
    end {
        Write-Output $ObjOutput
    }
}