Functions/Accounts/Add-PASAccount.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
function Add-PASAccount {
    <#
.SYNOPSIS
Adds a new privileged account to the Vault
Uses either the API present from 10.4 onwards, or the version 9 API endpoint.

.DESCRIPTION
Adds a new privileged account to the Vault.
Parameters are processed to create request object from passed parameters in the required format.

.PARAMETER name
The name of the account.
A version 10.4 onward specific parameter

.PARAMETER secretType
The type of password.
A version 10.4 onward specific parameter

.PARAMETER secret
The password value
A version 10.4 onward specific parameter

.PARAMETER platformAccountProperties
key-value pairs to associate with the account, as defined by the account platform.
These properties are validated against the mandatory and optional properties of the specified platform's definition.
A version 10.4 onward specific parameter

.PARAMETER automaticManagementEnabled
Whether CPM Password Management should be enabled
A version 10.4 onward specific parameter

.PARAMETER manualManagementReason
A reason for disabling CPM Password Management
A version 10.4 onward specific parameter

.PARAMETER remoteMachines
For supported platforms, a list of remote machines the account can connect to.
A version 10.4 onward specific parameter

.PARAMETER accessRestrictedToRemoteMachines
Whether access is restricted to the defined remote machines.
A version 10.4 onward specific parameter

.PARAMETER SafeName
The safe where the account will be created

.PARAMETER PlatformID
The CyberArk platform to assign to the account

.PARAMETER Address
The Address of the machine where the account will be used

.PARAMETER AccountName
The name of the account

.PARAMETER Password
The password value as a secure string

.PARAMETER Username
Username on the target machine

.PARAMETER DisableAutoMgmt
Whether or not automatic management wll be disabled for the account

.PARAMETER DisableAutoMgmtReason
The reason why automatic management wll be disabled for the account

.PARAMETER GroupName
A groupname with which the account will be associated

.PARAMETER GroupPlatformID
Group platform to base created group ID on, if ID doesn't exist

.PARAMETER Port
Port number over which the account will be used

.PARAMETER ExtraPass1Name
Logon account name

.PARAMETER ExtraPass1Folder
Folder where logon account is stored

.PARAMETER ExtraPass1Safe
Safe where logon account is stored

.PARAMETER ExtraPass3Name
Reconcile account name

.PARAMETER ExtraPass3Folder
Folder where reconcile account is stored

.PARAMETER ExtraPass3Safe
Safe where reconcile account is stored

.PARAMETER DynamicProperties
Hashtable of name=value pairs

.PARAMETER sessionToken
Hashtable containing the session token returned from New-PASSession

.PARAMETER WebSession
WebRequestSession object returned from New-PASSession

.PARAMETER BaseURI
PVWA Web Address
Do not include "/PasswordVault/"

.PARAMETER PVWAAppName
The name of the CyberArk PVWA Virtual Directory.
Defaults to PasswordVault

.PARAMETER ExternalVersion
The External CyberArk Version, returned automatically from the New-PASSession function from version 9.7 onwards.

.EXAMPLE
$token | Add-PASAccount -address ThisServer -userName ThisUser -platformID UNIXSSH -SafeName UNIXSafe -automaticManagementEnabled $false

Using the version 10 API, adds an account which is disbaled for automatic password management

.EXAMPLE
$token | Add-PASAccount -safe Prod_Access -PlatformID WINDOMAIN -Address domain.com -Password $secureString -username domainUser

Using the "version 9" API, adds account domain.com\domainuser to the Prod_Access Safe using the WINDOMAIN platform.
The contents of $secureString will be set as the password value.

.INPUTS
All parameters can be piped by property name

.OUTPUTS
None for v9
v10.4 outputs th details of the created account.

.NOTES

.LINK

#>

    [CmdletBinding()]
    param(

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [string]$name,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [string]$address,

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [string]$userName,

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [Alias("PolicyID")]
        [string]$platformID,

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [ValidateNotNullOrEmpty()]
        [Alias("safe")]
        [string]$SafeName,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [ValidateSet("Password", "Key")]
        [string]$secretType,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [securestring]$secret,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [hashtable]$platformAccountProperties,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [boolean]$automaticManagementEnabled,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [string]$manualManagementReason,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [string]$remoteMachines,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [boolean]$accessRestrictedToRemoteMachines,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [string]$accountName,

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [securestring]$password,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [boolean]$disableAutoMgmt,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [string]$disableAutoMgmtReason,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [string]$groupName,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [string]$groupPlatformID,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [int]$Port,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [ValidateNotNullOrEmpty()]
        [string]$ExtraPass1Name,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [string]$ExtraPass1Folder,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [ValidateNotNullOrEmpty()]
        [string]$ExtraPass1Safe,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [ValidateNotNullOrEmpty()]
        [string]$ExtraPass3Name,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [string]$ExtraPass3Folder,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [ValidateNotNullOrEmpty()]
        [string]$ExtraPass3Safe,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V9"
        )]
        [hashtable]$DynamicProperties,

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true
        )]
        [ValidateNotNullOrEmpty()]
        [hashtable]$sessionToken,

        [parameter(
            ValueFromPipelinebyPropertyName = $true
        )]
        [Microsoft.PowerShell.Commands.WebRequestSession]$WebSession,

        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true
        )]
        [string]$BaseURI,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true
        )]
        [string]$PVWAAppName = "PasswordVault",

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true
        )]
        [System.Version]$ExternalVersion = "0.0"
    )

    BEGIN {

        $MinimumVersion = [System.Version]"10.4"

        #The (version 9 API) Add Account JSON object requires specific formatting.
        #Different parameters are contained within the JSON at different depths.
        #Programmatic processing is required to format the JSON as required.

        #V9 baseparameters are contained in JSON object at the same depth
        $baseParameters = @("Safe", "PlatformID", "Address", "AccountName", "Password", "Username",
            "DisableAutoMgmt", "DisableAutoMgmtReason", "GroupName", "GroupPlatformID")

        #V10 parameters are nested under JSON object properties
        $remoteMachine = @("remoteMachines", "accessRestrictedToRemoteMachines")
        $SecretMgmt = @("automaticManagementEnabled", "manualManagementReason")

    }#begin

    PROCESS {

        #Get all parameters that will be sent in the request
        $boundParameters = $PSBoundParameters | Get-PASParameter

        #declare empty array to hold keys to remove from bound parameters
        [array]$keysToRemove = @()

        if ($PSCmdlet.ParameterSetName -eq "V10") {

            Assert-VersionRequirement -ExternalVersion $ExternalVersion -RequiredVersion $MinimumVersion

            #Create URL for Request
            $URI = "$baseURI/$PVWAAppName/api/Accounts"

            #deal with "secret" SecureString
            If ($PSBoundParameters.ContainsKey("secret")) {

                #Include decoded password in request
                $boundParameters["secret"] = $(ConvertTo-InsecureString -SecureString $secret)

            }

            $remoteMachinesAccess = @{ }
            $boundParameters.keys | Where-Object { $remoteMachine -contains $_ } | ForEach-Object {

                #add key=value to hashtable
                $remoteMachinesAccess[$_] = $boundParameters[$_]


            }

            $secretManagement = @{ }
            $boundParameters.keys | Where-Object { $SecretMgmt -contains $_ } | ForEach-Object {

                #add key=value to hashtable
                $secretManagement[$_] = $boundParameters[$_]

            }

            $boundParameters["remoteMachinesAccess"] = $remoteMachinesAccess
            $boundParameters["secretManagement"] = $secretManagement

            $body = $boundParameters |
            Get-PASParameter -ParametersToRemove @($remoteMachine + $SecretMgmt) |
            ConvertTo-Json -depth 4

        }

        if ($PSCmdlet.ParameterSetName -eq "V9") {

            #Create URL for Request
            $URI = "$baseURI/$PVWAAppName/WebServices/PIMServices.svc/Account"

            #deal with Password SecureString
            If ($PSBoundParameters.ContainsKey("password")) {

                #Include decoded password in request
                $boundParameters["password"] = $(ConvertTo-InsecureString -SecureString $password)

            }

            #Process for required formatting - fix V10 specific parameter names
            $boundParameters["safe"] = $SafeName
            $boundParameters["username"] = $userName

            $boundParameters.remove("SafeName")
            $boundParameters.remove("userName")
            #declare empty hashtable to hold "non-base" parameters
            $properties = @{ }

            #Get "non-base" parameters
            $boundParameters.keys | Where-Object { $baseParameters -notcontains $_ } | ForEach-Object {

                #For all "non-base" parameters except "DynamicProperties"
                if ($_ -ne "DynamicProperties") {

                    #Add key/Value to "properties" hashtable
                    $properties[$_] = $boundParameters[$_]

                }

                Else {
                    #for DynamicProperties key=value pairs

                    #Enumerate DynamicProperties object
                    $boundParameters[$_].getenumerator() | ForEach-Object {

                        #add key=value to "properties" hashtable
                        $properties[$_.name] = $_.value

                    }
                }

                #add the "non-base" parameter key to array
                $keysToRemove = $keysToRemove + $_

            }

            #Add "non-base" parameter hashtable as value of "properties" on boundparameters object
            $boundParameters["properties"] = @($properties.getenumerator() | ForEach-Object { $_ })

            #Create body of request
            $body = @{

                #account node does not contain non-base parameters
                "account" = $boundParameters | Get-PASParameter -ParametersToRemove $keysToRemove

                #ensure nodes at all required depths are included in the JSON object
            } | ConvertTo-Json -Depth 4

        }

        #send request to PAS web service
        $result = Invoke-PASRestMethod -Uri $URI -Method POST -Body $Body -Headers $sessionToken -WebSession $WebSession

        if ($PSCmdlet.ParameterSetName -eq "V10") {

            if ($result) {

                #Return Results
                $result | Add-ObjectDetail -typename "psPAS.CyberArk.Vault.Account.V10" -PropertyToAdd @{

                    "sessionToken"    = $sessionToken
                    "WebSession"      = $WebSession
                    "BaseURI"         = $BaseURI
                    "PVWAAppName"     = $PVWAAppName
                    "ExternalVersion" = $ExternalVersion

                }

            }

        }

    }#process

    END { }#end
}