-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.ps1
103 lines (95 loc) · 4.12 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
##################################################
# HelloID-Conn-Prov-Target-GoodHabitz-Delete
# PowerShell V2
##################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#region functions
function Resolve-GoodHabitzError {
[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
}
if (-not [string]::IsNullOrEmpty($ErrorObject.ErrorDetails.Message)) {
$httpErrorObj.ErrorDetails = $ErrorObject.ErrorDetails.Message
}
elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
if ($null -ne $ErrorObject.Exception.Response) {
$streamReaderResponse = [System.IO.StreamReader]::new($ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
if (-not [string]::IsNullOrEmpty($streamReaderResponse)) {
$httpErrorObj.ErrorDetails = $streamReaderResponse
}
}
}
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails
Write-Output $httpErrorObj
}
}
#endregion
try {
# Verify if [aRef] has a value
if ([string]::IsNullOrEmpty($($actionContext.References.Account))) {
throw 'The account reference could not be found'
}
if ($actionContext.DryRun -eq $true) {
Write-Information "[DryRun] Delete GoodHabitz account: [$($actionContext.References.Account)] for person: [$($personContext.Person.DisplayName)] will be executed during enforcement"
}
# Process
if (-not($actionContext.DryRun -eq $true)) {
Write-Information "Deleting GoodHabitz account with accountReference: [$($actionContext.Data.EmailAddress)]"
$splatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/api/person/forget?email=$($actionContext.Data.EmailAddress)"
Method = 'POST'
ContentType = 'application/x-www-form-urlencoded'
Headers = @{
Authorization = "Bearer $($ActionContext.Configuration.ApiKey)"
}
}
try {
$null = Invoke-RestMethod @splatParams
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Account [$($actionContext.Data.EmailAddress)] was successfully deleted"
IsError = $false
})
}
catch {
if ($_.Exception.Response.StatusCode -eq 404) {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Account [$($actionContext.Data.EmailAddress)] was not found, action skiped"
IsError = $false
})
}
else {
throw
}
}
}
$outputContext.success = $true
}
catch {
$outputContext.success = $false
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-GoodHabitzError -ErrorObject $ex
$auditMessage = "Could not delete GoodHabitz account. Error: $($errorObj.FriendlyMessage)"
Write-Warning "Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
}
else {
$auditMessage = "Could not delete GoodHabitz account. Error: $($_.Exception.Message)"
Write-Warning "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}