Public/Vlans.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#
# Copyright 2018, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-ArubaSWVlans {

    <#
        .SYNOPSIS
        Add a Vlan info on ArubaOS Switch (Provision)
 
        .DESCRIPTION
        Add vlan info (Id, Name, Voice, Snooping...)
 
        .EXAMPLE
        Add-ArubaSWVlans -id 85 -Name PowerArubaSW -is_voice_enabled -is_jumbo_enabled:$false
 
        Add vlan id 85 with name PowerArubaSW and enable voice vlan and disable jumbo
 
    #>


    Param(
        [Parameter (Mandatory = $true)]
        [int]$id,
        [Parameter (Mandatory = $false)]
        [string]$name,
        [Parameter (Mandatory = $false)]
        [switch]$is_voice_enabled,
        [Parameter (Mandatory = $false)]
        [switch]$is_jumbo_enabled,
        [Parameter (Mandatory = $false)]
        [switch]$is_dsnoop_enabled,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $uri = "rest/v4/vlans"

        $vlan = New-Object -TypeName PSObject

        $vlan | Add-Member -name "vlan_id" -membertype NoteProperty -Value $id

        if ( $PsBoundParameters.ContainsKey('name') ) {
            $vlan | Add-Member -name "name" -membertype NoteProperty -Value $name
        }
        else {
            #with APIv4, name is mandatory ... Set VLAN with number id
            $vlan | Add-Member -name "name" -membertype NoteProperty -Value "VLAN$($id)"
        }

        if ( $PsBoundParameters.ContainsKey('is_voice_enabled') ) {
            if ( $is_voice_enabled ) {
                $vlan | Add-Member -name "is_voice_enabled" -membertype NoteProperty -Value $True
            }
            else {
                $vlan | Add-Member -name "is_voice_enabled" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('is_jumbo_enabled') ) {
            if ( $is_jumbo_enabled ) {
                $vlan | Add-Member -name "is_jumbo_enabled" -membertype NoteProperty -Value $True
            }
            else {
                $vlan | Add-Member -name "is_jumbo_enabled" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('is_dsnoop_enabled') ) {
            if ( $is_dsnoop_enabled ) {
                $vlan | Add-Member -name "is_dsnoop_enabled" -membertype NoteProperty -Value $True
            }
            else {
                $vlan | Add-Member -name "is_dsnoop_enabled" -membertype NoteProperty -Value $false
            }
        }

        $response = Invoke-ArubaSWWebRequest -method "POST" -body $vlan -uri $uri -connection $connection
        $vlans = ($response.Content | ConvertFrom-Json)

        $vlans
    }

    End {
    }
}
function Get-ArubaSWVlans {

    <#
        .SYNOPSIS
        Get Vlans info about ArubaOS Switch (Provision)
 
        .DESCRIPTION
        Get Vlans Info (Id, Name, Voice, snooping...)
 
        .EXAMPLE
        Get-ArubaSWVlans
 
        Get ALL vlans on the switch
 
        .EXAMPLE
        Get-ArubaSWVlans Aruba
 
        Get info about vlan named Aruba on the switch
 
        .EXAMPLE
        Get-ArubaSWVlans -id 23
 
        Get info about vlan id 23 on the switch
 
    #>


    [CmdLetBinding(DefaultParameterSetName = "Default")]

    Param(
        [Parameter (Mandatory = $false, ParameterSetName = "id")]
        [int]$id,
        [Parameter (Mandatory = $false, ParameterSetName = "name", Position = 1)]
        [string]$Name,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        $uri = "rest/v4/vlans"

        $response = Invoke-ArubaSWWebRequest -method "GET" -uri $uri -connection $connection

        $vlans = ($response.Content | ConvertFrom-Json).vlan_element

        switch ( $PSCmdlet.ParameterSetName ) {
            "name" { $vlans | Where-Object { $_.name -match $name } }
            "id" { $vlans | Where-Object { $_.vlan_id -eq $id } }
            default { $vlans }
        }
    }

    End {
    }
}
function Set-ArubaSWVlans {

    <#
        .SYNOPSIS
        Configure Vlan info on ArubaOS Switch (Provision)
 
        .DESCRIPTION
        Configure vlan info (Id, Name, Voice, Snooping...)
 
        .EXAMPLE
        $vlan = Get-ArubaSWVlans -id 85
        PS C:\>$vlan | Set-ArubaSWVlans -Name PowerArubaSW -is_voice_enabled -is_jumbo_enabled:$false
 
        Configure vlan id 85 with name PowerArubaSW and enable voice vlan and disable jumbo
        .EXAMPLE
        Set-ArubaSWVlans -id 85 -Name PowerArubaSW2 -is_voice_enabled -is_dsnoop_enabled:$false
 
        Configure vlan id 85 with name PowerArubaSW2 and enable voice vlan and disable dsnoop
 
    #>


    Param(
        [Parameter (Mandatory = $true, ParameterSetName = "id")]
        [int]$id,
        [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "vlan")]
        #ValidateScript({ ValidateVlan $_ })]
        [psobject]$vlan,
        [Parameter (Mandatory = $false)]
        [string]$name,
        [Parameter (Mandatory = $false)]
        [switch]$is_voice_enabled,
        [Parameter (Mandatory = $false)]
        [switch]$is_jumbo_enabled,
        [Parameter (Mandatory = $false)]
        [switch]$is_dsnoop_enabled,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        #get vlan id from vlan ps object
        if ($vlan) {
            $id = $vlan.vlan_id
            $oldname = $vlan.name
        }
        $uri = "rest/v4/vlans/${id}"

        $_vlan = New-Object -TypeName PSObject

        #with APIv4, vlan_id is mandatory...
        $_vlan | Add-Member -name "vlan_id" -membertype NoteProperty -Value $id

        if ( $PsBoundParameters.ContainsKey('name') ) {
            $_vlan | Add-Member -name "name" -membertype NoteProperty -Value $name
        }
        else {
            #with APIv4, name is also mandatory... (why ?!!) use already configured name.
            if (!$oldname) {
                #if you don't pipelining (and use -id), need to get vlan name...
                $oldname = (Get-ArubaSWVlans -id $id).name
            }

            $_vlan | Add-Member -name "name" -membertype NoteProperty -Value $oldname
        }
        $name

        if ( $PsBoundParameters.ContainsKey('is_voice_enabled') ) {
            if ( $is_voice_enabled ) {
                $_vlan | Add-Member -name "is_voice_enabled" -membertype NoteProperty -Value $True
            }
            else {
                $_vlan | Add-Member -name "is_voice_enabled" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('is_jumbo_enabled') ) {
            if ( $is_jumbo_enabled ) {
                $_vlan | Add-Member -name "is_jumbo_enabled" -membertype NoteProperty -Value $True
            }
            else {
                $_vlan | Add-Member -name "is_jumbo_enabled" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('is_dsnoop_enabled') ) {
            if ( $is_dsnoop_enabled ) {
                $_vlan | Add-Member -name "is_dsnoop_enabled" -membertype NoteProperty -Value $True
            }
            else {
                $_vlan | Add-Member -name "is_dsnoop_enabled" -membertype NoteProperty -Value $false
            }
        }

        $response = Invoke-ArubaSWWebRequest -method "PUT" -body $_vlan -uri $uri -connection $connection
        $rep_vlan = ($response.Content | ConvertFrom-Json)

        $rep_vlan
    }

    End {
    }
}

function Remove-ArubaSWVlans {

    <#
        .SYNOPSIS
        Remove a Vlan on ArubaOS Switch (Provision)
 
        .DESCRIPTION
        Remove vlan on the switch
 
        .EXAMPLE
        $vlan = Get-ArubaSWVlans -id 85
        PS C:\>$vlan | Remove-ArubaSWVlans
 
        Remove vlan id 85
 
        .EXAMPLE
        Remove-ArubaSWVlans -id 85 -noconfirm
 
        Remove vlan id 85 with no confirmation
    #>


    Param(
        [Parameter (Mandatory = $true, ParameterSetName = "id")]
        [int]$id,
        [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "vlan")]
        #ValidateScript({ ValidateVlan $_ })]
        [psobject]$vlan,
        [Parameter(Mandatory = $false)]
        [switch]$noconfirm,
        [Parameter (Mandatory=$False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection=$DefaultArubaSWConnection
    )

    Begin {
    }

    Process {

        #get vlan id from vlan ps object
        if ($vlan) {
            $id = $vlan.vlan_id
        }

        $uri = "rest/v4/vlans/${id}"

        if ( -not ( $Noconfirm )) {
            $message = "Remove Vlan on switch"
            $question = "Proceed with removal of vlan ${id} ?"
            $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))

            $decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
        }
        else { $decision = 0 }
        if ($decision -eq 0) {
            Write-Progress -activity "Remove Vlan"
            $null = Invoke-ArubaSWWebRequest -method "DELETE" -uri $uri -connection $connection
            Write-Progress -activity "Remove Vlan" -completed
        }
    }

    End {
    }
}