Functions/Accounts/New-PASAccountObject.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
Function New-PASAccountObject {
    <#
    .SYNOPSIS
    Creates hashtable structured to be used as input for add account operations

    .DESCRIPTION
    Provide parameter values to return hashtable structured to be used as input for add account operations.

    .PARAMETER uploadIndex
    The numeric identifier for the account.

    .PARAMETER userName
    Username on the target machine

    .PARAMETER name
    The name of the account.

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

    .PARAMETER platformID
    The CyberArk platform to assign to the account

    .PARAMETER SafeName
    The safe where the account will be created

    .PARAMETER secretType
    The type of password.

    .PARAMETER secret
    The password value

    .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.

    .PARAMETER automaticManagementEnabled
    Whether CPM Password Management should be enabled

    .PARAMETER manualManagementReason
    A reason for disabling CPM Password Management

    .PARAMETER remoteMachines
    For supported platforms, a list of remote machines the account can connect to.

    .PARAMETER accessRestrictedToRemoteMachines
    Whether access is restricted to the defined remote machines.

    .PARAMETER groupName
    Group to associate the account with

    .EXAMPLE
    New-PASAccountObject -userName SomeAccount1 -address domain.com -platformID WinDomain -SafeName SomeSafe

    Returns hashtable structured to be used as input for add account operations

    .LINK
    https://pspas.pspete.dev/commands/New-PASAccountObject

    #>

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'remoteMachinesAccess', Justification = "False Positive")]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'secretManagement', Justification = "False Positive")]
    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = "V10")]
    param(

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = "V10"
        )]
        [int]$uploadIndex,

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

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

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

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

        [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 = "V10"
        )]
        [string]$groupName

    )

    Begin {

        #V10 parameters are nested under JSON object properties
        $remoteMachine = [Collections.Generic.List[String]]@("remoteMachines", "accessRestrictedToRemoteMachines")
        $SecretMgmt = [Collections.Generic.List[String]]@("automaticManagementEnabled", "manualManagementReason")

    }

    Process {

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

        switch ($PSCmdlet.ParameterSetName) {

            "V10" {

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

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

                }

                $boundParameters.keys | Where-Object { $remoteMachine -contains $PSItem } | ForEach-Object {

                    $remoteMachinesAccess = @{ }

                } {

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


                } {

                    $boundParameters["remoteMachinesAccess"] = $remoteMachinesAccess

                }

                $boundParameters.keys | Where-Object { $SecretMgmt -contains $PSItem } | ForEach-Object {

                    $secretManagement = @{ }

                } {

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

                } {

                    $boundParameters["secretManagement"] = $secretManagement

                }

                if ($PSCmdlet.ShouldProcess($userName, "Create Account Object Definition")) {

                    $boundParameters | Get-PASParameter -ParametersToRemove @($remoteMachine + $SecretMgmt)

                }

                break

            }

        }

    }

    End {}

}