Functions/Safes/Get-PASSafe.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
# .ExternalHelp psPAS-help.xml
function Get-PASSafe {
    [CmdletBinding(DefaultParameterSetName = 'Gen2')]
    param(
        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2'
        )]
        [ValidateNotNullOrEmpty()]
        [string]$search,

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

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2'
        )]
        [ValidateNotNullOrEmpty()]
        [Boolean]$includeAccounts,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen2'
        )]
        [ValidateNotNullOrEmpty()]
        [Boolean]$extendedDetails,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $true,
            ParameterSetName = 'Gen1-byName'
        )]
        [ValidateNotNullOrEmpty()]
        [string]$SafeName,

        [parameter(
            Mandatory = $false,
            ValueFromPipelinebyPropertyName = $false,
            ParameterSetName = 'Gen1-byQuery'
        )]
        [string]$query,

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

        [parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $false
        )]
        [int]$TimeoutSec
    )

    BEGIN {

        $typeName = 'psPAS.CyberArk.Vault.Safe'
        $Limit = 25   #default if you call the API with no value

    }#begin

    PROCESS {

        $boundParameters = $PSBoundParameters | Get-PASParameter

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

        switch ($PSCmdlet.ParameterSetName) {

            'Gen2' {

                #assign new type name
                $typeName = "$typeName.Gen2"

                switch ($PSBoundParameters) {

                    ( { $PSItem.ContainsKey('extendedDetails') }) {

                        #check required version
                        Assert-VersionRequirement -RequiredVersion 12.1

                        If ($extendedDetails -eq $false) {

                            #assign new type name
                            $typeName = "$typeName.Name"

                        }

                    }

                    default {

                        Assert-VersionRequirement -RequiredVersion 12.0

                    }

                }

                $returnProperty = 'value'

                #define base URL
                $URI = "$Script:BaseURI/API/Safes"

                If ($null -ne $queryString) {

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

                }

                break

            }

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

                #check required version
                Assert-VersionRequirement -MaximumVersion 12.2

                #Create URL for Gen1 API requests
                $URI = "$Script:BaseURI/WebServices/PIMServices.svc/Safes"

            }

            'Gen1-byName' {

                $returnProperty = 'GetSafeResult'

                #Build URL from base URL
                $URI = "$URI/$($SafeName | Get-EscapedString)"

                break

            }

            'Gen1-byQuery' {

                $returnProperty = 'SearchSafesResult'

                if ($null -ne $queryString) {

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

                }

                break

            }

            'Gen1-byAll' {

                $returnProperty = 'GetSafesResult'

                break

            }

        }

        #send request to web service
        $result = Invoke-PASRestMethod -Uri $URI -Method GET -WebSession $Script:WebSession -TimeoutSec $TimeoutSec

        switch ($PSCmdlet.ParameterSetName) {

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

                $Total = $result.Total

                If ($Total -gt 0) {

                    $Safes = [Collections.Generic.List[Object]]::New(($result.$returnProperty))

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

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

                    }

                    $return = $Safes

                }

                ElseIf ($null -ne $result) {

                    $return = $result.$returnProperty

                }

                break

            }

            'Gen2' {

                #return list
                $return = $Result | Get-NextLink -TimeoutSec $TimeoutSec

                break

            }

        }

        if ($null -ne $return) {

            #Return Results
            $return | Add-ObjectDetail -typename $typeName

        }

    }#process

    END { }#end

}