forked from dstamen/PowerCLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-LLDPCDPInfo.ps1
125 lines (111 loc) · 4.42 KB
/
Get-LLDPCDPInfo.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#########################################################################################################
## LLDP_CDP_Information.ps1
## Version 1.0
## Auxilium Technology AS, November 2014
## http://www.auxilium.no
##
## Description:
## Sometimes it's useful to know which ports your host(s) are connected to. It's also good to know what
## kind of switch you have to deal with and if possible, get the firmware version.
##
## Script created 20.11.2014
## Bjørn-Ove Kiil ([email protected])
##
## Requirement:
## The switches where your host(s) are connected have to support Link Layer Discovery Protocol (LLDP) or
## Cisco Discovery Protocol (CDP) and this feature must be enabled in order to get any information out
## of it.
## PS! LLDP is only available on host(s) which are a member of a Distributed Virtual Switch!!
##
## Usage:
## .\LLDP_CDP_Information.ps1 <Clustername>
##
## Ex.: .\LLDP_CDP_Information.ps1 LabCluster
##
#########################################################################################################
# Connect-VIserver your-vcenter-server -user your-user -password your-password
param($Cluster) ## Input from command line
if(!($Cluster))
{
Write-Host -Fore YELLOW "Missing parameter!"
Write-Host -Fore YELLOW "Usage:"
Write-Host -Fore YELLOW ".\LLDP_CDP_Information.ps1 <your cluster>"
Write-Host -Fore YELLOW ""
Write-Host -Fore YELLOW "Example: .\LLDP_CDP_Information.ps1 LabCluster"
exit
}
if(!(Get-Cluster $Cluster -EA SilentlyContinue))
{
Write-Host -Fore RED "No cluster found with the name: $Cluster "
Pause
Write-Host -Fore YELLOW "These clusters where found in the vCenter you have connected to:"
Get-Cluster | sort Name | Select Name
exit
}
$vmh = Get-Cluster $Cluster | Get-VMHost | sort name
$LLDPResultArray = @()
$CDPResultArray = @()
If ($vmh.ConnectionState -eq "Connected" -or $vmh.State -eq "Maintenance")
{
Get-View $vmh.ID | `
% { $esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} | `
% { foreach ($physnic in $_.NetworkInfo.Pnic) {
$pnicInfo = $_.QueryNetworkHint($physnic.Device)
foreach( $hint in $pnicInfo )
{
## If the switch support LLDP, and you're using Distributed Virtual Swicth with LLDP
if ($hint.LLDPInfo)
{
#$hint.LLDPInfo.Parameter
$LLDPResult = "" | select-object VMHost, PhysicalNic, PhysSW_Port, PhysSW_Name, PhysSW_Description, PhysSW_MGMTIP, PhysSW_MTU
$LLDPResult.VMHost = $esxname
$LLDPResult.PhysicalNic = $physnic.Device
$LLDPResult.PhysSW_Port = ($hint.LLDPInfo.Parameter | ? { $_.Key -eq "Port Description" }).Value
$LLDPResult.PhysSW_Name = ($hint.LLDPInfo.Parameter | ? { $_.Key -eq "System Name" }).Value
$LLDPResult.PhysSW_Description = ($hint.LLDPInfo.Parameter | ? { $_.Key -eq "System Description" }).Value
$LLDPResult.PhysSW_MGMTIP = ($hint.LLDPInfo.Parameter | ? { $_.Key -eq "Management Address" }).Value
$LLDPResult.PhysSW_MTU = ($hint.LLDPInfo.Parameter | ? { $_.Key -eq "MTU" }).Value
$LLDPResultArray += $LLDPResult
}
## If it's a Cisco switch behind the server ;)
if ($hint.ConnectedSwitchPort)
{
#$hint.ConnectedSwitchPort
$CDPResult = "" | select-object VMHost, PhysicalNic, PhysSW_Port, PhysSW_Name, PhysSW_HWPlatform, PhysSW_Software, PhysSW_MGMTIP, PhysSW_MTU
$CDPResult.VMHost = $esxname
$CDPResult.PhysicalNic = $physnic.Device
$CDPResult.PhysSW_Port = $hint.ConnectedSwitchPort.PortID
$CDPResult.PhysSW_Name = $hint.ConnectedSwitchPort.DevID
$CDPResult.PhysSW_HWPlatform = $hint.ConnectedSwitchPort.HardwarePlatform
$CDPResult.PhysSW_Software = $hint.ConnectedSwitchPort.SoftwareVersion
$CDPResult.PhysSW_MGMTIP = $hint.ConnectedSwitchPort.MgmtAddr
$CDPResult.PhysSW_MTU = $hint.ConnetedSwitchPort.Mtu
$CDPResultArray += $CDPResult
}
if(!($hint.LLDPInfo) -and (!($hint.ConnectedSwitchPort)))
{
Write-Host -Fore YELLOW "No CDP or LLDP information available! "
Write-Host -Fore YELLOW "Check if your switches support these protocols and if"
Write-Host -Fore YELLOW "the CDP/LLDP features are enabled."
}
}
}
}
}
else
{
Write-Host "No host(s) found in Connected or Maintenance state!"
exit
}
## Output to screen and/or file
if ($CDPResultArray)
{
$CDPResultArray | ft -autosize
$CDPResultArray | Export-Csv .\CDP_Info_$Cluster.txt -useculture -notypeinformation
}
if ($LLDPResultArray)
{
$LLDPResultArray | ft -autosize
$LLDPResultArray | Export-Csv .\LLDP_Info_$Cluster.txt -useculture -notypeinformation
}
#disconnect-viserver * -Confirm:$false