-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable.ps1
104 lines (96 loc) · 3.76 KB
/
disable.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
#####################################################
# HelloID-Conn-Prov-Target-IrisIntranet-Disable
#
# Version: 1.0.0.0
#####################################################
$VerbosePreference = 'Continue'
# Initialize default value's
$config = $configuration | ConvertFrom-Json
$personObj = $person | ConvertFrom-Json
$aRef = $AccountReference | ConvertFrom-Json
$success = $false
$auditLogs = New-Object Collections.Generic.List[PSCustomObject]
#region Helper Functions
function Resolve-HTTPError {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
$HttpErrorObj = @{
FullyQualifiedErrorId = $ErrorObject.FullyQualifiedErrorId
MyCommand = $ErrorObject.InvocationInfo.MyCommand
RequestUri = $ErrorObject.TargetObject.RequestUri
}
if ($ErrorObject.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') {
$HttpErrorObj['ErrorMessage'] = $ErrorObject.ErrorDetails.Message
} elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
$stream = $ErrorObject.Exception.Response.GetResponseStream()
$stream.Position = 0
$streamReader = New-Object System.IO.StreamReader $Stream
$errorResponse = $StreamReader.ReadToEnd()
$HttpErrorObj['ErrorMessage'] = $errorResponse
}
Write-Output "'$($HttpErrorObj.ErrorMessage)', TargetObject: '$($HttpErrorObj.RequestUri), InvocationCommand: '$($HttpErrorObj.MyCommand)"
}
}
#endregion
if (-not($dryRun -eq $true)) {
try {
[System.Collections.Generic.List[object]]$operations = @()
$operations.Add(
[PSCustomObject]@{
op = "Replace"
path = "active"
value = $false
}
)
$body = [ordered]@{
schemas = @(
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
)
Operations = $operations
} | ConvertTo-Json
Write-Verbose 'Adding Authorization headers'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $($config.ApiToken)")
$splatParams = @{
Uri = "$($config.BaseUrl)/api/iris/v1/$($config.ApiID)/scim/Users/$aRef"
Headers = $headers
Body = $body
Method = 'Patch'
}
$results = Invoke-RestMethod @splatParams
if ($results.id){
$logMessage = "Account '$($aRef)' for '$($personObj.DisplayName)' successfully disabled"
Write-Verbose $logMessage
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = $logMessage
IsError = $False
})
}
} catch {
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorMessage = Resolve-HTTPError -Error $ex
$auditMessage = "Account '$($aRef)' for '$($personObj.DisplayName)' not disabled. Error: $errorMessage"
} else {
$auditMessage = "Account '$($aRef)' for '$($personObj.DisplayName)' not disabled. Error: $($ex.Exception.Message)"
}
$auditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
Write-Error $auditMessage
}
}
$result = [PSCustomObject]@{
Success = $success
Account = $account
AuditDetails = $auditMessage
}
Write-Output $result | ConvertTo-Json -Depth 10