This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Get-ICXVLAN.ps1
305 lines (257 loc) · 11.7 KB
/
Get-ICXVLAN.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Get-ICXVLAN.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Get VLAN(s) from a Brocade ICX Switch
# Repository : https://github.com/BornToBeRoot/PowerShell_BrocadeICX
###############################################################################################################
<#
.SYNOPSIS
Get VLAN(s) from a Brocade ICX Switch
.DESCRIPTION
Get VLAN(s) from a Brocade ICX Switch as PSCustomObject, which can be further processed.
.EXAMPLE
Get-ICXVLAN -ComputerName megatron | ? {$_.Name -eq "Test1"} | ft
ID Name By TaggedPort UntaggedPort
-- ---- -- ---------- ------------
1001 Test1 port {0/1/4} {0/1/37, 0/1/39, 0/1/45, 0/1/46}
.EXAMPLE
New-ICXSession -ComputerName MEGATRON, megatron
Get-ICXVLAN -Session (Get-ICXSession) | ? {$_.Name -eq "Test1"} | ft
SessionID ComputerName ID Name By TaggedPort UntaggedPort
--------- ------------ -- ---- -- ---------- ------------
0 MEGATRON 1001 Test1 port {0/1/4} {0/1/37, 0/1/39, 0/1/45, 0/1/46}
1 megatron 1001 Test1 port {0/1/4} {0/1/37, 0/1/39, 0/1/45, 0/1/46}
.LINK
https://github.com/BornToBeRoot/PowerShell_BrocadeICX/blob/master/Documentation/Function/Get-ICXVLAN.README.md
#>
function Get-ICXVLAN
{
[CmdletBinding(DefaultParameterSetName='ComputerName')]
param(
[Parameter(
ParameterSetName='ComputerName',
Position=0,
Mandatory=$true,
HelpMessage='Hostname or IPv4-Address of the Brocade ICX Switch')]
[String[]]$ComputerName,
[Parameter(
ParameterSetName='Session',
Position=0,
ValueFromPipeline=$true,
Mandatory=$true,
HelpMessage='Brocade ICX session')]
[pscustomobject[]]$Session,
[Parameter(
ParameterSetName='ComputerName',
Position=1,
HelpMessage='Accept the SSH key')]
[switch]$AcceptKey,
[Parameter(
ParameterSetName='ComputerName',
Position=2,
HelpMessage='Credentials to authenticate agains the Brocade ICX Switch (SSH connection)')]
[System.Management.Automation.PSCredential]
[System.Management.Automation.CredentialAttribute()]
$Credential
)
Begin{
function get_ICXVLAN {
param(
$Session,
$DefaultDisplaySet
)
Begin{
}
Process{
$Output = (Invoke-ICXCommand -Command "show running-config" -Session $Session).Output
### $Output
##################################
# vlan 1 name DEFAULT-VLAN by port
# !
# vlan 1001 name Test1 by port
# tagged ethe 0/1/1
# untagged ethe 0/1/5 to 0/1/10
# !
# vlan 1002 name Test2 by port
# tagged ethe 0/1/1 to 0/1/4
# untagged ethe 0/1/11 to 0/1/20
# !
$VLAN_Detected = $false
$VLAN_TaggedPort = @()
$VLAN_UntaggedPort = @()
# Parse the output and create a pscustomobject
foreach($Line in $Output)
{
$Line = $Line.Trim()
if($Line.StartsWith("vlan"))
{
$VLAN_Detected = $true
Write-Verbose "VLAN found in line: ""$Line"""
$Line_Split = $Line.Split(" ")
### $Line_Split
###############
# vlan
# 1
# name
# Test1
# by
# port
for($i = 0; $i -lt ($Line_Split.Count - 1); $i++)
{
if($Line_Split[$i] -eq "vlan")
{
[int]$VLAN_ID = $Line_Split[$i + 1]
}
elseif($Line_Split[$i] -eq "name")
{
$VLAN_Name = $Line_Split[$i + 1]
}
elseif($Line_Split[$i] -eq "by")
{
$VLAN_By = $Line_Split[$i + 1]
}
}
}
elseif($Line.StartsWith("tagged"))
{
Write-Verbose "Tagged ports found in line: $Line"
# Remove " to " and replace it with "-", then split
$Line_Split = $Line.Replace(" to ", "-").Split(" ")
### $Line_Split
#############
# tagged
# ethe
# 0/1/1-0/1/4
for($i = 0; $i -lt ($Line_Split.Count - 1); $i++)
{
if($Line_Split[$i] -eq "ethe")
{
if($Line_Split[$i + 1].Contains("-"))
{
$StackID = $Line_Split[$i + 1].Split("-")[0].Split("/")[0]
$Slot = $Line_Split[$i + 1].Split("-")[0].Split("/")[1]
$StartPort = $Line_Split[$i + 1].Split("-")[0].Split("/")[2]
$EndPort = $Line_Split[$i + 1].Split("-")[1].Split("/")[2]
foreach($Port in $StartPort..$EndPort)
{
$VLAN_TaggedPort += [String]::Format("{0}/{1}/{2}", $StackID, $Slot, $Port)
}
}
else
{
$VLAN_TaggedPort += $Line_Split[$i + 1]
}
}
}
}
elseif($Line.StartsWith("untagged"))
{
Write-Verbose "Untagged ports found in line: $Line"
# Remove " to " and replace it with "-", then split
$Line_Split = $Line.Replace(" to ", "-").Split(" ")
### $Line_Split
###############
# untagged
# ethe
# 0/1/5-0/1/10
for($i = 0; $i -lt ($Line_Split.Count - 1); $i++)
{
if($Line_Split[$i] -eq "ethe")
{
if($Line_Split[$i + 1].Contains("-"))
{
$StackID = $Line_Split[$i + 1].Split("-")[0].Split("/")[0]
$Slot = $Line_Split[$i + 1].Split("-")[0].Split("/")[1]
$StartPort = $Line_Split[$i + 1].Split("-")[0].Split("/")[2]
$EndPort = $Line_Split[$i + 1].Split("-")[1].Split("/")[2]
foreach($Port in $StartPort..$EndPort)
{
$VLAN_UntaggedPort += [String]::Format("{0}/{1}/{2}", $StackID, $Slot, $Port)
}
}
else
{
$VLAN_UntaggedPort += $Line_Split[$i + 1]
}
}
}
}
elseif($VLAN_Detected -and $Line.StartsWith("!"))
{
$VLAN_Detected = $false
Write-Verbose "End of VLAN: $VLAN_ID ($VLAN_Name)!"
$ICXVLAN = [pscustomobject] @{
SessionID = $Session.SessionID
ComputerName = $Session.ComputerName
Id = $VLAN_ID
Name = $VLAN_Name
By = $VLAN_By
TaggedPort = $VLAN_TaggedPort
UntaggedPort = $VLAN_UntaggedPort
}
# Set the default parameter set
$ICXVLAN.PSObject.TypeNames.Insert(0,'BrocadeICX.ICXVLAN')
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultDisplaySet)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
$ICXVLAN | Add-Member MemberSet PSStandardMembers $PSStandardMembers
$ICXVLAN
# Clear the variale(s)/array(s)
$VLAN_Name = [String]::Empty
$VLAN_By = [String]::Empty
$VLAN_TaggedPort = @()
$VLAN_UntaggedPort = @()
}
}
}
End{
}
}
}
Process{
$DefaultDisplaySet = 'ID', 'Name', 'By', 'TaggedPort', 'UntaggedPort'
if($Session.Count -gt 1 -or $ComputerName.Count -gt 1)
{
$DefaultDisplaySet = 'SessionID', 'ComputerName' + $DefaultDisplaySet
}
switch($PSCmdlet.ParameterSetName)
{
"ComputerName" {
if($Credential -eq $null)
{
# If no credentials are submitted by parameter, prompt the user to enter them
try{
$Credential = Get-Credential $null
}
catch{
throw "Entering credentials has been canceled by user. Can't establish SSH connection without credentials!"
}
}
foreach($ComputerName2 in $ComputerName)
{
$ICXSession = New-ICXSession -ComputerName $ComputerName2 -AcceptKey:$AcceptKey -Credential $Credential
if($null -ne $ICXSession)
{
get_ICXVLAN -Session $ICXSession -DefaultDisplaySet $DefaultDisplaySet
Remove-ICXSession -Session $ICXSession
}
}
}
"Session" {
foreach($Session2 in $Session)
{
if(Test-ICXSession -Session $Session2)
{
get_ICXVLAN -Session $Session2 -DefaultDisplaySet $DefaultDisplaySet
}
else
{
Write-Error -Message "Session ($Session2) is not a valid Brocade ICX session or not managed by the BrocadeICX module!" -Category ConnectionError
}
}
}
}
}
End{
}
}