Functions/SafeMembers/Get-PASSafeMember.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
# .ExternalHelp psPAS-help.xml
function Get-PASSafeMember {
    [CmdletBinding(DefaultParameterSetName = 'Gen2')]
    param(
        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2'
        )]
        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2-MemberFilter'
        )]
        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen1-SafeMembers'
        )]
        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen1-MemberPermissions'
        )]
        [ValidateNotNullOrEmpty()]
        [string]$SafeName,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2-MemberFilter'
        )]
        [ValidateSet('user', 'group')]
        [string]$memberType,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2-MemberFilter'
        )]
        [boolean]$membershipExpired,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2-MemberFilter'
        )]
        [boolean]$includePredefinedUsers,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2-MemberFilter'
        )]
        [string]$search,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2-MemberFilter'
        )]
        [ValidateSet('asc', 'desc')]
        [string]$sort,

        [Alias('UserName')]
        [parameter(
            Mandatory = $true,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen1-MemberPermissions'
        )]
        [ValidateNotNullOrEmpty()]
        [string]$MemberName,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $false,
            ParameterSetName = 'Gen2'
        )]
        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $false,
            ParameterSetName = 'Gen2-MemberFilter'
        )]
        [int]$TimeoutSec,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $false,
            ParameterSetName = 'Gen1-SafeMembers'
        )]
        [switch]$UseGen1API
    )

    BEGIN {

        $Request = @{ }
        $Method = 'GET'
        $Limit = 25   #default if you call the API with no value

    }#begin

    PROCESS {

        switch ($PSCmdlet.ParameterSetName) {

            ( { $PSItem -match '^Gen1-' } ) {

                #Create URL for Gen1 API requests
                $URI = "$Script:BaseURI/WebServices/PIMServices.svc/Safes/$($SafeName | Get-EscapedString)/Members"

            }

            ( { $PSItem -match '^Gen2' } ) {

                #Create URL for Gen1 API requests
                Assert-VersionRequirement -RequiredVersion 12.0

                #Create URL for Gen2 API requests
                $URI = "$Script:BaseURI/api/Safes/$($SafeName | Get-EscapedString)/Members"

            }

            'Gen1-MemberPermissions' {

                #check required version
                Assert-VersionRequirement -MaximumVersion 12.3

                #Create URL for member specific request
                $URI = "$URI/$($MemberName | Get-EscapedString)"
                #Send a PUT Request instead of GET
                $Method = 'PUT'
                #Send an empty body
                #Add to Request parameters for PUT Request
                $Request['Body'] = @{'member' = @{ } } | ConvertTo-Json

                break

            }

            'Gen2-MemberFilter' {

                Assert-VersionRequirement -RequiredVersion 12.1

                #Get Parameters to include in request
                $boundParameters = $PSBoundParameters | Get-PASParameter -ParametersToRemove SafeName, memberType, membershipExpired, includePredefinedUsers, TimeoutSec
                $filterParameters = $PSBoundParameters | Get-PASParameter -ParametersToKeep memberType, membershipExpired, includePredefinedUsers
                $FilterString = $filterParameters | ConvertTo-FilterString

                If ($null -ne $FilterString) {

                    $boundParameters = $boundParameters + $FilterString

                }

                #Create Query String, escaped for inclusion in request URL
                $queryString = $boundParameters | ConvertTo-QueryString

                If ($null -ne $queryString) {

                    #Build URL from base URL
                    $URI = "$URI`?$queryString"

                }

                $Request['TimeoutSec'] = $TimeoutSec

                break

            }

        }

        #Build Request Parameters
        $Request['URI'] = $URI
        $Request['Method'] = $Method
        $Request['WebSession'] = $Script:WebSession

        #Send request to webservice
        $result = Invoke-PASRestMethod @Request

        If ($null -ne $result) {

            switch ($PSCmdlet.ParameterSetName) {

                'Gen1-MemberPermissions' {

                    #format output
                    $Output = $result.member | Select-Object MembershipExpirationDate,

                    @{Name = 'UserName'; 'Expression' = { $MemberName } },

                    @{Name = 'Permissions'; 'Expression' = {

                            $result.member.permissions | ConvertFrom-KeyValuePair }

                    }

                }

                'Gen1-SafeMembers' {

                    #output
                    $Output = $result.members | Select-Object UserName, Permissions

                }

                ( { $PSItem -match '^Gen1-' } ) {

                    #Format and add SafeName to Gen 1 Output
                    $Output = $Output | Add-ObjectDetail -typename psPAS.CyberArk.Vault.Safe.Member -PropertyToAdd @{

                        'SafeName' = $SafeName

                    }

                    break

                }

                ( { $PSItem -match '^Gen2' } ) {

                    $Total = $result.Count

                    If ($Total -gt 0) {

                        $Members = [Collections.Generic.List[Object]]::New(($result.value))

                        For ( $Offset = $Limit ; $Offset -lt $Total ; $Offset += $Limit ) {

                            $Null = $Members.AddRange((Invoke-PASRestMethod -Uri "$URI`?limit=$Limit&OffSet=$Offset" -Method GET -WebSession $Script:WebSession -TimeoutSec $TimeoutSec).value)

                        }

                        $Output = $Members

                    }

                    ElseIf ($null -ne $result) {

                        $Output = $result.value

                    }

                    $Output = $Output |
                        Select-Object *, @{Name = 'UserName'; 'Expression' = { $PSItem.MemberName } } |
                        Add-ObjectDetail -typename psPAS.CyberArk.Vault.Safe.Member.Gen2

                    break

                }

            }

            $Output

        }

    }#process

    END { }#end

}