Public/LocalUser.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#
# Copyright 2022 Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-ArubaCPLocaluser {

    <#
        .SYNOPSIS
        Add a Local User on ClearPass

        .DESCRIPTION
        Add a Local User with user_id, username, password, role...

        .EXAMPLE
        Add-ArubaCPLocaluser -user_id MyPowerArubaCP_userid -password ( ConvertTo-SecureString MyPassword -AsPlainText -Force ) -role_name "[Employee]"

        Add a Local User with user_id MyPowerArubaCP_userid (same username) and role_name [Employee]

        .EXAMPLE
        Add-ArubaCPLocaluser -user_id MyPowerArubaCP_userid -username MyPowerArubaCP_username -password ( ConvertTo-SecureString MyPassword -AsPlainText -Force ) -role_name "[Employee]"

        Add a Local User with user_id MyPowerArubaCP_userid, username MyPowerArubaCP_username and role_name [Employee]

        .EXAMPLE
        $mysecurepassword = ConvertTo-SecureString MyPassword -AsPlainText -Force
        PS >$attributes = @{ "Sponsor" = "PowerArubaCP" }
        PS >Add-ArubaCPLocaluser -user_id MyPowerArubaCP_userid -password $mysecurepassword -role_name "[Employee]" -attributes $attributes

        Add a Local User with user_id MyPowerArubaCP_userid (same username), role_name [Employee] with Sponsor Attributes to PowerArubaCP
    #>


    Param(
        [Parameter (Mandatory = $false)]
        [int]$id,
        [Parameter (Mandatory = $true)]
        [string]$user_id,
        [Parameter (Mandatory = $false)]
        [string]$username,
        [Parameter (Mandatory = $true)]
        [securestring]$password,
        [Parameter (Mandatory = $true)]
        [string]$role_name,
        [Parameter (Mandatory = $false)]
        [switch]$enabled,
        [Parameter (Mandatory = $false)]
        [switch]$change_pwd_next_login,
        [Parameter (Mandatory = $false)]
        [psobject]$attributes,
        [Parameter (Mandatory = $False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection = $DefaultArubaCPConnection
    )

    Begin {
    }

    Process {

        $uri = "api/local-user"

        $_lu = new-Object -TypeName PSObject

        if ( $PsBoundParameters.ContainsKey('id') ) {
            $_lu | add-member -name "id" -membertype NoteProperty -Value $id
        }

        $_lu | add-member -name "user_id" -membertype NoteProperty -Value $user_id

        if ( $PsBoundParameters.ContainsKey('username') ) {
            $_lu | add-member -name "username" -membertype NoteProperty -Value $username
        }
        else {
            #if don't define username use the user_id
            $_lu | add-member -name "username" -membertype NoteProperty -Value $user_id
        }

        $credentials = New-Object System.Net.NetworkCredential("", $password)
        $_lu | add-member -name "password" -membertype NoteProperty -Value $credentials.Password

        $_lu | add-member -name "role_name" -membertype NoteProperty -Value $role_name

        if ( $PsBoundParameters.ContainsKey('enabled') ) {
            if ($enabled) {
                $_lu | add-member -name "enabled" -membertype NoteProperty -Value $true
            }
            else {
                $_lu | add-member -name "enabled" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('change_pwd_next_login') ) {
            if ($change_pwd_next_login) {
                $_lu | add-member -name "change_pwd_next_login" -membertype NoteProperty -Value $true
            }
            else {
                $_lu | add-member -name "change_pwd_next_login" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('attributes') ) {
            $_lu | add-member -name "attributes" -membertype NoteProperty -Value $attributes
        }

        $lu = invoke-ArubaCPRestMethod -method "POST" -body $_lu -uri $uri -connection $connection
        $lu
    }

    End {
    }
}

function Get-ArubaCPLocaluser {

    <#
        .SYNOPSIS
        Get Local user info on CPPM

        .DESCRIPTION
        Get Local User (user_id, username, password, roles)

        .EXAMPLE
        Get-ArubaCPLocalUser

        Get ALL Local User on the Clearpass

        .EXAMPLE
        Get-ArubaCPLocalUser MyPowerArubaCP_userid

        Get info about Local User ID MyPowerArubaCP_userid on the ClearPass

        .EXAMPLE
        Get-ArubaCPLocalUser -id 23

        Get info about Local User id 23 on the ClearPass

        .EXAMPLE
        Get-ArubaCPLocalUser -username MyPowerArubaCP -filter_type contains

        Get info about Local User username where name contains MyPowerArubaCP

       .EXAMPLE
        Get-ArubaCPLocalUser -filter_attribute role -filter_type contains -filter_value Employee

        Get info about Local User where role contains Employee
    #>


    [CmdLetBinding(DefaultParameterSetName = "Default")]

    Param(
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "id")]
        [int]$id,
        [Parameter (Mandatory = $false, Position = 1)]
        [Parameter (ParameterSetName = "user_id")]
        [string]$user_id,
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "username")]
        [string]$username,
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "filter")]
        [string]$filter_attribute,
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "id")]
        [Parameter (ParameterSetName = "user_id")]
        [Parameter (ParameterSetName = "username")]
        [Parameter (ParameterSetName = "filter")]
        [ValidateSet('equal', 'contains')]
        [string]$filter_type,
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "filter")]
        [psobject]$filter_value,
        [Parameter (Mandatory = $false)]
        [int]$limit,
        [Parameter (Mandatory = $False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection = $DefaultArubaCPConnection
    )

    Begin {
    }

    Process {

        $invokeParams = @{ }
        if ( $PsBoundParameters.ContainsKey('limit') ) {
            $invokeParams.add( 'limit', $limit )
        }

        switch ( $PSCmdlet.ParameterSetName ) {
            "id" {
                $filter_value = $id
                $filter_attribute = "id"
            }
            "user_id" {
                $filter_value = $user_id
                $filter_attribute = "user_id"
            }
            "username" {
                $filter_value = $username
                $filter_attribute = "username"
            }
            default { }
        }

        if ( $PsBoundParameters.ContainsKey('filter_type') ) {
            switch ( $filter_type ) {
                "equal" {
                    $filter_value = @{ "`$eq" = $filter_value }
                }
                "contains" {
                    $filter_value = @{ "`$contains" = $filter_value }
                }
                default { }
            }
        }

        if ($filter_value -and $filter_attribute) {
            $filter = @{ $filter_attribute = $filter_value }
            $invokeParams.add( 'filter', $filter )
        }

        $uri = "api/local-user"

        $lu = Invoke-ArubaCPRestMethod -method "GET" -uri $uri @invokeParams -connection $connection

        $lu._embedded.items
    }

    End {
    }
}


function Set-ArubaCPLocalUser {

    <#
        .SYNOPSIS
        Configure a Local User on ClearPass

        .DESCRIPTION
        Configure a Local User on ClearPass

        .EXAMPLE
        $lu = Get-ArubaCPLocalUser -username MyPowerArubaCP
        PS C:\>$lu | Set-ArubaCPLocalUser -username MyPowerArubaCP2

        Change username for user(name) MyPowerArubaCP

        .EXAMPLE
        $lu = Get-ArubaCPLocalUser -username MyPowerArubaCP
        PS C:\>$lu | Set-ArubaCPLocalUser -password ( ConvertTo-SecureString MyPassword -AsPlainText -Force )

        Change Password for user(name) MyPowerArubaCP

        .EXAMPLE
        $lu = Get-ArubaCPLocalUser -username MyPowerArubaCP
        PS C:\>$lu | Set-ArubaCPLocalUser -role "[Guest]"

        Change Role (Guest) for user(name) MyPowerArubaCP

    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
    Param(
        [Parameter (Mandatory = $true, ParameterSetName = "id")]
        [int]$id,
        [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "ep")]
        [ValidateScript( { Confirm-ArubaCPLocalUser $_ })]
        [psobject]$lu,
        [Parameter (Mandatory = $false)]
        [string]$user_id,
        [Parameter (Mandatory = $false)]
        [string]$username,
        [Parameter (Mandatory = $false)]
        [securestring]$password,
        [Parameter (Mandatory = $false)]
        [string]$role_name,
        [Parameter (Mandatory = $false)]
        [switch]$enabled,
        [Parameter (Mandatory = $false)]
        [switch]$change_pwd_next_login,
        [Parameter (Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection = $DefaultArubaCPConnection
    )

    Begin {
    }

    Process {

        #get lu id from Local User ps object
        if ($lu) {
            $id = $lu.id
        }

        $uri = "api/local-user/${id}"
        $_lu = new-Object -TypeName PSObject

        if ( $PsBoundParameters.ContainsKey('user_id') ) {
            $_lu | add-member -name "user_id" -membertype NoteProperty -Value $user_id
        }

        if ( $PsBoundParameters.ContainsKey('username') ) {
            $_lu | add-member -name "username" -membertype NoteProperty -Value $username
        }

        if ( $PsBoundParameters.ContainsKey('password') ) {
            $credentials = New-Object System.Net.NetworkCredential("", $password)
            $_lu | add-member -name "password" -membertype NoteProperty -Value $credentials.Password
        }

        if ( $PsBoundParameters.ContainsKey('role_name') ) {
            $_lu | add-member -name "role_name" -membertype NoteProperty -Value $role_name
        }

        if ( $PsBoundParameters.ContainsKey('enabled') ) {
            if ( $enabled ) {
                $_lu | add-member -name "enabled" -membertype NoteProperty -Value $True
            }
            else {
                $_lu | add-member -name "enabled" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('change_pwd_next_login') ) {
            if ( $change_pwd_next_login ) {
                $_lu | add-member -name "change_pwd_next_login" -membertype NoteProperty -Value $True
            }
            else {
                $_lu | add-member -name "change_pwd_next_login" -membertype NoteProperty -Value $false
            }
        }

        if ($PSCmdlet.ShouldProcess($id, 'Configure Local User')) {
            $lu = Invoke-ArubaCPRestMethod -method "PATCH" -body $_lu -uri $uri -connection $connection
            $lu
        }

    }

    End {
    }
}

function Remove-ArubaCPLocalUser {

    <#
        .SYNOPSIS
        Remove a Local User on ClearPass

        .DESCRIPTION
        Remove a Local User on ClearPass

        .EXAMPLE
        $lu = Get-ArubaCPLocalUser -username MyPowerArubaCP_username
        PS C:\>$lu | Remove-ArubaCPLocalUser

        Remove a Local User with user name MyPowerArubaCP_username

        .EXAMPLE
        Remove-ArubaCPLocalUser -id 3001 -confirm:$false

        Remove a Local User with id 3001 and no confirmation
    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'high')]
    Param(
        [Parameter (Mandatory = $true, ParameterSetName = "id")]
        [int]$id,
        [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "ep")]
        [ValidateScript( { Confirm-ArubaCPLocalUser $_ })]
        [psobject]$lu,
        [Parameter (Mandatory = $False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection = $DefaultArubaCPConnection
    )

    Begin {
    }

    Process {

        #get Local User id from lp ps object
        if ($lu) {
            $id = $lu.id
            $user_id = "(" + $lu.user_id + ")"
        }

        $uri = "api/local-user/${id}"

        if ($PSCmdlet.ShouldProcess("$id $user_id", 'Remove Local User')) {
            Invoke-ArubaCPRestMethod -method "DELETE" -uri $uri -connection $connection
        }

    }

    End {
    }

}