-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.ps1
249 lines (219 loc) · 8.68 KB
/
delete.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
##################################################
# HelloID-Conn-Prov-Target-Zivver-Delete
# PowerShell V2
##################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
# Set debug logging
switch ($actionContext.Configuration.isDebug) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
#region functions
function Invoke-ZivverRestMethod {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Method,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Endpoint,
[object]
$Body,
[string]
$ContentType = 'application/json',
[Parameter(Mandatory)]
[System.Collections.IDictionary]
$Headers
)
process {
try {
$splatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/api/scim/v2/$Endpoint"
Headers = $Headers
Method = $Method
ContentType = $ContentType
}
if ($Body) {
Write-Verbose 'Adding body to request'
$utf8Encoding = [System.Text.Encoding]::UTF8
$encodedBody = $utf8Encoding.GetBytes($body)
$splatParams['Body'] = $encodedBody
}
Invoke-RestMethod @splatParams -Verbose:$false
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
function Resolve-ZivverError {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[object]
$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
ScriptLineNumber = $ErrorObject.InvocationInfo.ScriptLineNumber
Line = $ErrorObject.InvocationInfo.Line
ErrorDetails = $ErrorObject.Exception.Message
FriendlyMessage = $ErrorObject.Exception.Message
}
try {
$errorDetails = $ErrorObject.ErrorDetails.Message
$httpErrorObj.ErrorDetails = "Exception: $($ErrorObject.Exception.Message), Error: $($errorDetails)"
$httpErrorObj.FriendlyMessage = "Error: $($errorDetails)"
}
catch {
$httpErrorObj.FriendlyMessage = "Received an unexpected response. The JSON could not be converted, error: [$($_.Exception.Message)]. Original error from web service: [$($ErrorObject.Exception.Message)]"
}
Write-Output $httpErrorObj
}
}
#endregion functions
try {
#region Verify account reference
$actionMessage = "verifying account reference"
if ([string]::IsNullOrEmpty($($actionContext.References.Account))) {
throw "The account reference could not be found"
}
#endregion Verify account reference
#region Create authorization headers
$actionMessage = "creating authorization headers"
$headers = [System.Collections.Generic.Dictionary[[String], [String]]]::new()
$headers.Add("Authorization", "Bearer $($actionContext.Configuration.Token)")
#endregion Create authorization headers
#region Get Zivver account
$actionMessage = "querying Zivver account"
$getZivverSplatParams = @{
Headers = $headers
Endpoint = "Users/$($actionContext.References.Account)"
Method = 'GET'
}
try {
$correlatedAccount = Invoke-ZivverRestMethod @getZivverSplatParams
}
catch {
# A '400'bad request is returned if the entity cannot be found
if ($_.Exception.Response.StatusCode -eq 400) {
$correlatedAccount = $null
}
else {
throw
}
}
Write-Information "Queried Ziver account where [id] = [$($actionContext.References.Account)]. Result: $($correlatedAccount | ConvertTo-Json)"
#endregion Get Zivver account
#region Calulate action
$actionMessage = "calculating action"
if (($correlatedAccount | Measure-Object).count -eq 1) {
if ([string]$correlatedAccount.active -eq $actionContext.Data.active) {
$actionAccount = "NoChanges"
}
else {
$actionAccount = "Disable"
}
}
elseif (($correlatedAccount | Measure-Object).count -eq 0) {
$actionAccount = "NotFound"
}
#endregion Calulate action
#region Process
switch ($actionAccount) {
"Disable" {
#region Update account
$actionMessage = "disabling account"
$body = @{
"schemas" = @(
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
"urn:ietf:params:scim:schemas:zivver:0.1:User"
)
"active" = $actionContext.Data.active
}
$putZivverSplatParams = @{
Headers = $headers
Endpoint = "Users/$($actionContext.References.Account)"
Method = 'PUT'
Body = $body | ConvertTo-Json
}
if (-Not($actionContext.DryRun -eq $true)) {
$null = Invoke-ZivverRestMethod @putZivverSplatParams
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Account with userName [$($correlatedAccount.userName)] and AccountReference [$($actionContext.References.Account)] disabled."
IsError = $false
})
}
else {
Write-Warning "DryRun: Would disable account with AccountReference: $($outputContext.AccountReference | ConvertTo-Json)."
}
break
}
"NoChanges" {
#region No changes
$actionMessage = "disabling account"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Account with userName [$($correlatedAccount.userName)] and AccountReference [$($actionContext.References.Account)] is already disabled."
IsError = $false
})
#endregion No changes
break
}
"NotFound" {
#region No account found
$actionMessage = "disabling account"
# If account is not found on delete the action is skipped
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Account with AccountReference [$($actionContext.References.Account)] not found (skipped action). Possibly indicating that it could be deleted, or not correlated."
IsError = $false
})
#endregion No account found
break
}
}
#endregion Process
}
catch {
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-ZivverError -ErrorObject $ex
$auditMessage = "Error $($actionMessage). Error: $($errorObj.FriendlyMessage)"
Write-Verbose "Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
}
else {
$auditMessage = "Error $($actionMessage). Error: $($ex.Exception.Message)"
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}
finally {
# Check if auditLogs contains errors, if no errors are found, set success to true
if ($outputContext.AuditLogs.IsError -contains $true) {
$outputContext.Success = $false
}
else {
$outputContext.Success = $true
# Change mapping here
# Define your mapping here for returning the correct data to HelloID
$outputDataObject = [PSCustomObject]@{
active = [string]$actionContext.Data.active # value is returned as boleaan
}
Write-Verbose "output data to HelloID: [$($outputDataObject | Convertto-json)]"
$outputContext.Data = $outputDataObject
# Define your mapping here for returning the correct previous data to HelloID
$outputPreviousDataObject = [PSCustomObject]@{
active = [string]$correlatedAccount.active # value is returned as boleaan
}
Write-Verbose "output previous data to HelloID: [$($outputPreviousDataObject | Convertto-json)]"
$outputContext.PreviousData = $outputPreviousDataObject
}
}