-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.ps1
316 lines (276 loc) · 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#####################################################
# HelloID-Conn-Prov-Target-Azure-Guest-Account-Delete
#
# Version: 1.1.0
#####################################################
# Initialize default values
$c = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json
$success = $false
$auditLogs = [System.Collections.Generic.List[PSCustomObject]]::new()
# The accountReference object contains the Identification object provided in the create account call
$aRef = $accountReference | ConvertFrom-Json
# Set TLS to accept TLS, TLS 1.1 and TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
# Set debug logging
switch ($($c.isDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
$InformationPreference = "Continue"
$WarningPreference = "Continue"
# Used to connect to Azure AD using Microsoft Graph
$AADtenantID = $c.AADtenantID
$AADAppId = $c.AADAppId
$AADAppSecret = $c.AADAppSecret
# Check if aRef available
$aRefMissing = $false
if ([String]::IsNullOrEmpty($aRef)) {
$aRefMissing = $true
Write-Warning "aRef has a null or empty value"
}
#region functions
function New-AuthorizationHeaders {
[CmdletBinding()]
param(
[parameter(Mandatory)]
[string]
$TenantId,
[parameter(Mandatory)]
[string]
$ClientId,
[parameter(Mandatory)]
[string]
$ClientSecret
)
try {
Write-Verbose "Creating Access Token"
$baseUri = "https://login.microsoftonline.com/"
$authUri = $baseUri + "$TenantId/oauth2/token"
$body = @{
grant_type = "client_credentials"
client_id = "$ClientId"
client_secret = "$ClientSecret"
resource = "https://graph.microsoft.com"
}
$Response = Invoke-RestMethod -Method POST -Uri $authUri -Body $body -ContentType 'application/x-www-form-urlencoded'
$accessToken = $Response.access_token
#Add the authorization header to the request
Write-Verbose 'Adding Authorization headers'
$headers = [System.Collections.Generic.Dictionary[[String], [String]]]::new()
$headers.Add('Authorization', "Bearer $accesstoken")
$headers.Add('Accept', 'application/json')
$headers.Add('Content-Type', 'application/json')
# Needed to filter on specific attributes (https://docs.microsoft.com/en-us/graph/aad-advanced-queries)
$headers.Add('ConsistencyLevel', 'eventual')
Write-Output $headers
}
catch {
throw $_
}
}
function Resolve-HTTPError {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
FullyQualifiedErrorId = $ErrorObject.FullyQualifiedErrorId
MyCommand = $ErrorObject.InvocationInfo.MyCommand
RequestUri = $ErrorObject.TargetObject.RequestUri
ScriptStackTrace = $ErrorObject.ScriptStackTrace
ErrorMessage = ''
}
if ($ErrorObject.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') {
$httpErrorObj.ErrorMessage = $ErrorObject.ErrorDetails.Message
}
elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
$httpErrorObj.ErrorMessage = [System.IO.StreamReader]::new($ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
}
Write-Output $httpErrorObj
}
}
function Resolve-MicrosoftGraphAPIErrorMessage {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
try {
$errorObjectConverted = $ErrorObject | ConvertFrom-Json -ErrorAction Stop
if ($null -ne $errorObjectConverted.error_description) {
$errorMessage = $errorObjectConverted.error_description
}
elseif ($null -ne $errorObjectConverted.error) {
if ($null -ne $errorObjectConverted.error.message) {
$errorMessage = $errorObjectConverted.error.message
if ($null -ne $errorObjectConverted.error.code) {
$errorMessage = $errorMessage + " Error code: $($errorObjectConverted.error.code)"
}
}
else {
$errorMessage = $errorObjectConverted.error
}
}
else {
$errorMessage = $ErrorObject
}
}
catch {
$errorMessage = $ErrorObject
}
Write-Output $errorMessage
}
}
#endregion functions
try {
if ($aRefMissing -eq $true) {
if (-not($dryRun -eq $true)) {
$auditLogs.Add([PSCustomObject]@{
# Action = "DeleteAccount" # Optional
Message = "aRef incomplete, cannot delete account"
IsError = $true
})
}
else {
Write-Warning "DryRun: aRef incomplete, cannot delete account. aRef object: $($aRef | ConvertTo-Json -Depth 10)"
}
}
elseif ($incompleteAccount -eq $true) {
if (-not($dryRun -eq $true)) {
$auditLogs.Add([PSCustomObject]@{
# Action = "DeleteAccount" # Optional
Message = "Account object incomplete, cannot delete account"
IsError = $true
})
}
else {
Write-Warning "DryRun: Account object incomplete, cannot delete account. Account object: $($account | ConvertTo-Json -Depth 10)"
}
}
else {
# Get current Azure AD account
try {
$headers = New-AuthorizationHeaders -TenantId $AADtenantID -ClientId $AADAppId -ClientSecret $AADAppSecret
Write-Verbose "Querying Azure AD account with id '$($aRef)'"
$baseUri = "https://graph.microsoft.com/"
$splatWebRequest = @{
Uri = "$baseUri/v1.0/users/$aRef"
Headers = $headers
Method = 'GET'
}
$currentAccount = $null
$currentAccount = Invoke-RestMethod @splatWebRequest -Verbose:$false
if ($null -ne $currentAccount.id) {
Write-Verbose "Successfully found Azure AD account id '$($aRef)': $($currentAccount.userPrincipalName) ($($currentAccount.id))"
}
else {
throw "No account found in Azure AD with id '$($aRef)'"
}
}
catch {
# Clean up error variables
$verboseErrorMessage = $null
$auditErrorMessage = $null
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObject = Resolve-HTTPError -Error $ex
$verboseErrorMessage = $errorObject.ErrorMessage
$auditErrorMessage = Resolve-MicrosoftGraphAPIErrorMessage -ErrorObject $errorObject.ErrorMessage
}
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($verboseErrorMessage)) {
$verboseErrorMessage = $ex.Exception.Message
}
if ([String]::IsNullOrEmpty($auditErrorMessage)) {
$auditErrorMessage = $ex.Exception.Message
}
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
if ($auditErrorMessage -Like "No account found in Azure AD with id '$($aRef)'" -or ($auditErrorMessage -like "*Error code: Request_ResourceNotFound*" -and $auditErrorMessage -like "*$($aRef)*") ) {
$auditLogs.Add([PSCustomObject]@{
# Action = "DeleteAccount" # Optional
Message = "No Azure AD account found with id '$($aRef)'. Possibly already deleted, skipping action."
IsError = $false
})
}
else {
$auditLogs.Add([PSCustomObject]@{
# Action = "DeleteAccount" # Optional
Message = "Error querying Azure AD account with id '$($aRef)'. Error Message: $auditErrorMessage"
IsError = $true
})
}
}
if (-NOT($auditLogs.IsError -contains $true)) {
# Delete Azure AD account
try {
$baseUri = "https://graph.microsoft.com/"
$splatWebRequest = @{
Uri = "$baseUri/v1.0/users/$($currentAccount.id)"
Headers = $headers
Method = 'DELETE'
}
if (-not($dryRun -eq $true)) {
Write-Verbose "Deleting Azure AD account '$($currentAccount.userPrincipalName) ($($currentAccount.id))'"
$deleteAccountResponse = $null
$deleteAccountResponse = Invoke-RestMethod @splatWebRequest -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
# Action = "DeleteAccount" # Optional
Message = "Successfully deleted Azure AD account '$($currentAccount.userPrincipalName) ($($currentAccount.id))'"
IsError = $false
})
}
else {
Write-Warning "DryRun: Would delete Azure AD account '$($currentAccount.userPrincipalName) ($($currentAccount.id))'"
}
}
catch {
# Clean up error variables
$verboseErrorMessage = $null
$auditErrorMessage = $null
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObject = Resolve-HTTPError -Error $ex
$verboseErrorMessage = $errorObject.ErrorMessage
$auditErrorMessage = Resolve-MicrosoftGraphAPIErrorMessage -ErrorObject $errorObject.ErrorMessage
}
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($verboseErrorMessage)) {
$verboseErrorMessage = $ex.Exception.Message
}
if ([String]::IsNullOrEmpty($auditErrorMessage)) {
$auditErrorMessage = $ex.Exception.Message
}
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$auditLogs.Add([PSCustomObject]@{
# Action = "DeleteAccount" # Optional
Message = "Error deleting Azure AD account '$($currentAccount.userPrincipalName) ($($currentAccount.id))'. Error Message: $auditErrorMessage"
IsError = $true
})
}
}
}
}
finally {
# Check if auditLogs contains errors, if no errors are found, set success to true
if (-NOT($auditLogs.IsError -contains $true) -and $aRefMissing -eq $false) {
$success = $true
}
# Send results
$result = [PSCustomObject]@{
Success = $success
AccountReference = $aRef
AuditLogs = $auditLogs
Account = $account
# Optionally return data for use in other systems
# ExportData = $exportData # There is no data left after the delete
}
Write-Output ($result | ConvertTo-Json -Depth 10)
}