I like having documentation, but I hate creating documentation. I’ll be the first to admit that I’m slightly lazy at times, however, my own personal preferences for what I’ll call “Effort Allocation”, are not the root of my dislike for creating documentation. The issue really stems from the fact that creating it is very time consuming, tedious, and usually lower on the priority list.
However, sometimes it’s not you that failed to create the documentation. Consultants frequently fall into this category.
The issue I’m handling here is documenting CDP information from the perspective of ESXi hosts using PowerCLI.
Rather than navigating to each host and grabbing the CDP info from each host in a cluster like the screenshot below, you can run the commandlet I’m publishing in this article.
Download the PS1 file, and check out examples in the comment based help below.
References: VMware KB 1007069
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
126
127
128
129
130
131
132
133
134
135
|
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2014 v4.1.61
Created on: 7/25/2014 1:00 PM
Created by: Jon Howe
Filename: Get-CDP.ps1
===========================================================================
.SYNOPSIS
Connects to VirtualCenter and lists pertinent CDP information
.DESCRIPTION
Connects to VirtualCenter and lists pertinent CDP information
.PARAMETER $VirtualCenterServer
Required String Parameter. The fully qualified domain name of the virtualcenter server
.PARAMETER $cluster
Optional StringParameter. The name of the cluster you want to filter by.
.PARAMETER $asLocalUser
Optional Boolean Parameter. Do you want to connect to vC as you, or do you want to manually
authenticate as a different user.
.EXAMPLE
Get-CDP -VirtualCenterServer vc-tst-1.test.in
Actions Taken:
This will connect to the specified virtualcenter server and list the CDP Information
Results:
ESXi_HostName : ESXi-1.test.in
VMnic : vmnic1
SwitchIdentity : test-3750.test.in
ManagementIP : 10.1.1.1
SwitchPort : GigabitEthernet1/0/1
SwitchPlatform : cisco WS-C3750G-48TS
Interfce_Vlan : 999
ESXi_HostName : ESXi-1.test.in
VMnic : vmnic2
SwitchIdentity : test-3750.test.in
ManagementIP : 10.1.1.1
SwitchPort : GigabitEthernet1/0/2
SwitchPlatform : cisco WS-C3750G-48TS
Interfce_Vlan : 999
.EXAMPLE
Get-CDP -VirtualCenterServer vc-tst-1.test.in -cluster testCluster -asLocalUser $false
Actions Taken:
This will connect you to the specified VirtualCenter server and only search hosts specified in
the cluster. It will prompt you to enter your username and password.
Results:
Same as example 1
.EXAMPLE
Get-CDP -VirtualCenterServer vc-tst-1.test.in | export-csv -path c:\temp\cdp_information.csv -notypeinformation
Actions Taken:
This script outputs an object, so you can do anything you want with the output, such as create a CSV, sort, etc.
Results:
Sames as example 1
.LINK
http://45.63.13.214/vmware-powercli-cdp/
.LINK
http://kb.vmware.com/kb/1007069
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[System.String]
$VirtualCenterServer,
[Parameter(Position = 1)]
[System.String]
$ClusterName,
[Parameter(Position = 2)]
[System.Boolean]
$asLocalUser=$true
)
#region Add Snapin and Connect to vC
#Check to see if the VMware.VimAutomation.Core snapin is loaded - load it if it's not
if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null)
{
Add-PsSnapin VMware.VimAutomation.Core
}
#Check to see if we're already connected to the correct VC Server
if ($DefaultVIServers.name -ne $VirtualCenterServer)
{
#Determine if we're logging in to VirtualCenter as a local user or if we should prompt for credentials
if ($asLocalUser)
{
Connect-VIServer -Server $VirtualCenterServer | Out-Null
Write-Debug "Logging in as local user to vc: $VirtualCenterServer"
}
else
{
Connect-VIServer -Server $VirtualCenterServer -Credential (Get-Credential) | Out-Null
Write-Debug "Logging in as manually selected user to vc: $VirtualCenterServer"
}
}
else
{
Write-Debug "Looks like we're already connected to: $VirtualCenterServer in this session"
}
#endregion Add Snapin and Connect to vC
$cdpDetails = @()
#Get List of Hosts
if ($ClusterName)
{
$hosts = Get-Cluster -Name $ClusterName | Get-VMHost | Where-Object { $_.ConnectionState -eq "Connected" }
}
else
{
$hosts = Get-VMHost | Where-Object { $_.ConnectionState -eq "Connected" }
}
#Get the Network Config information from each host
$hosts | % { Get-View $_.ID } | % { $esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem } |
% {
foreach ($physnic in $_.NetworkInfo.Pnic)
{
#Get detailed CDP information for all interfaces
$result = ($_.QueryNetworkHint($physnic.Device)).ConnectedSwitchPort
if ($result)
{
$objOneHost = New-Object System.Object
$objOneHost | Add-Member -type NoteProperty -name ESXi_HostName -Value $esxname
$objOneHost | Add-Member -type NoteProperty -name VMnic -Value $physnic.Device
$objOneHost | Add-Member -type NoteProperty -name SwitchIdentity -Value $result.DevId
$objOneHost | Add-Member -type NoteProperty -name ManagementIP -Value $result.Address
$objOneHost | Add-Member -type NoteProperty -name SwitchPort -Value $result.PortID
$objOneHost | Add-Member -type NoteProperty -name SwitchPlatform -Value $result.HardwarePlatform
$objOneHost | Add-Member -type NoteProperty -name Interfce_Vlan -Value $result.Vlan
$cdpDetails += $objOneHost
}
}
}
$cdpDetails
Disconnect-VIServer -Server $VirtualCenterServer | Out-Null
|