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
/
Copy pathGet-ICXInterface.ps1
187 lines (160 loc) · 7.06 KB
/
Get-ICXInterface.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
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Get-ICXInterface.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Get interface(s) from a Brocade ICX Switch
# Repository : https://github.com/BornToBeRoot/PowerShell_BrocadeICX
###############################################################################################################
<#
.SYNOPSIS
Get interface(s) from a Brocade ICX Switch
.DESCRIPTION
Get interface(s) from a Brocade ICX Switch with status [up|down], speed [1G|100M|10M] etc.
.EXAMPLE
Get-ICXInterface -ComputerName megatron | Select-Object -First 5 | Format-Table
Port Link State Duplex Speed Trunk Tag Pvid Priority MAC Name
---- ---- ----- ------ ----- ----- --- ---- -------- --- ----
0/1/1 Up Forward Full 1G None Yes N/A 0 0000.0000.0000 UPLINK
0/1/2 Up Forward Full 1G None No 1001 0 0000.0000.0001
0/1/3 Up Forward Full 100M None No 1001 0 0000.0000.0002
0/1/4 Down None None None None No 1 0 0000.0000.0003
0/1/5 Up Forward Full 1G None No 1001 0 0000.0000.0004
.LINK
https://github.com/BornToBeRoot/PowerShell_BrocadeICX/blob/master/Documentation/Function/Get-ICXInterface.README.md
#>
function Get-ICXInterface
{
[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_ICXInterface {
param(
$Session,
$DefaultDisplaySet
)
$Output = (Invoke-ICXCommand -Command "show interface brief" -Session $Session).Output
foreach($Line in $Output)
{
# Only process lines that start with "x\x\xx"
if($Line -match "^[0-9]\/[0-9]\/[0-9]{1,2}")
{
# Line looks like this "x/x/xx Down None None None None No 1 0 xxxx.xxxx.xxxx
# Replace white spaces and split it
$Line_Split = ($Line -replace '\s+', ' ').Split(" ")
### $Line_Split
###############
# Port
# Link
# State
# Duplex
# Speed
# Trunk
# Tag
# Pvid
# Priority
# MAC
# Name
# If port-name has spaces...
$Name = [String]::Empty
foreach($Line_Name in $Line_Split[10..($Line_Split.Count -1)])
{
$Name += $Line_Name + " "
}
$ICXInterface = [pscustomobject] @{
SessionID = $Session.SessionID
ComputerName = $Session.ComputerName
Port = $Line_Split[0]
Link = $Line_Split[1]
State = $Line_Split[2]
Duplex = $Line_Split[3]
Speed = $Line_Split[4]
Trunk = $Line_Split[5]
Tagged = $Line_Split[6]
Pvid = $Line_Split[7]
Priority = $Line_Split[8]
MAC = $Line_Split[9]
Name = $Name.TrimEnd()
}
# Set the default parameter set
$ICXInterface.PSObject.TypeNames.Insert(0,'BrocadeICX.ICXInterface')
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultDisplaySet)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
$ICXInterface | Add-Member MemberSet PSStandardMembers $PSStandardMembers
$ICXInterface
}
}
}
}
Process{
$DefaultDisplaySet = 'Port', 'Link', 'State', 'Duplex', 'Speed', 'Trunk', 'Tagged', 'Pvid', 'Priority', 'MAC', 'Name'
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_ICXInterface -Session $ICXSession -DefaultDisplaySet $DefaultDisplaySet
Remove-ICXSession -Session $ICXSession
}
}
}
"Session" {
foreach($Session2 in $Session)
{
if(Test-ICXSession -Session $Session2)
{
get_ICXInterface -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{
}
}