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
/
Test-ICXVLAN.ps1
147 lines (121 loc) · 4.37 KB
/
Test-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
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Get-ICXVLAN.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Test if a VLAN exist on a Brocade ICX Switch
# Repository : https://github.com/BornToBeRoot/PowerShell_BrocadeICX
###############################################################################################################
<#
.SYNOPSIS
Test if a VLAN exist on a Brocade ICX Switch
.DESCRIPTION
Test if a VLAN exist on a Brocade ICX Switch.
.EXAMPLE
Test-ICXVLAN -ComputerName megatron -VlanId 1
True
.EXAMPLE
$Cred = Get-Credentials $null
Test-ICXVLAN -ComputerName megatron -VlanId 2 -Credentials $Cred
False
.LINK
https://github.com/BornToBeRoot/PowerShell_BrocadeICX/blob/master/Documentation/Function/Get-ICXVLAN.README.md
#>
function Test-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,
Mandatory=$true,
HelpMessage='VLAN ID which is to tested')]
[Parameter(
ParameterSetName='Session',
Position=1,
Mandatory=$true,
HelpMessage='VLAN ID which is to tested')]
[Int32]$VlanId,
[Parameter(
ParameterSetName='ComputerName',
Position=2,
HelpMessage='Accept the SSH key')]
[switch]$AcceptKey,
[Parameter(
ParameterSetName='ComputerName',
Position=3,
HelpMessage='Credentials to authenticate agains the Brocade ICX Switch (SSH connection)')]
[System.Management.Automation.PSCredential]
[System.Management.Automation.CredentialAttribute()]
$Credential
)
Begin{
function test_ICXVLAN {
param(
$Session,
$VlanId
)
Begin{
}
Process{
[Int32[]]$VlanIds = (Get-ICXVLAN -Session $Session).Id
return $VlanIds.Contains($VlanId)
}
End{
}
}
}
Process{
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)
{
test_ICXVLAN -Session $ICXSession -VlanId $VlanId
Remove-ICXSession -Session $ICXSession
}
}
}
"Session" {
foreach($Session2 in $Session)
{
if(Test-ICXSession -Session $Session2)
{
test_ICXVLAN -Session $Session2 -VlanId $VlanId
}
else
{
Write-Error -Message "Session ($Session2) is not a valid Brocade ICX session or not managed by the BrocadeICX module!" -Category ConnectionError
}
}
}
}
}
End{
}
}