-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable.ps1
156 lines (143 loc) · 5.49 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
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
#####################################################
# HelloID-Conn-Prov-Target-DormakabaExos-Disable
#
# Version: 1.0.0
#####################################################
# Initialize default values
$config = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json
$aRef = $AccountReference | ConvertFrom-Json
$success = $false
$auditLogs = [System.Collections.Generic.List[PSCustomObject]]::new()
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
# Set debug logging
switch ($($config.IsDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
#region functions
function Get-AuthorizationHeaders {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Username,
[Parameter(Mandatory)]
[string]
$Password,
[Parameter(Mandatory)]
[string]
$BaseUrl
)
try {
Write-Verbose 'Get Identifier'
$pair = "$($Username):$($Password)"
$encodedCredsUsername = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$splatRestMethod = @{
Uri = "$BaseUrl/ExosApiLogin/api/v1.0/login"
Method = 'POST'
Headers = @{
Authorization = "Basic $encodedCredsUsername"
}
}
$identifier = (Invoke-RestMethod @splatRestMethod -Verbose:$false).value.Identifier
Write-Verbose 'Set Authorization Headers'
$pair = "MyApiKey:$identifier"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
Write-Output @{
Authorization = "Basic $encodedCreds"
'Content-Type' = 'application/json;charset=utf-8'
Accept = 'application/json;charset=utf-8'
}
} catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
#endregion functions
try {
# Add an auditMessage showing what will happen during enforcement
if ($dryRun -eq $true) {
$auditLogs.Add([PSCustomObject]@{
Message = "Disable DormakabaExos account for: [$($p.DisplayName)] will be executed during enforcement"
})
}
if (-not($dryRun -eq $true)) {
Write-Verbose "Disabling DormakabaExos account with accountReference: [$aRef]"
$splatAuthHeaders = @{
Username = $config.UserName
Password = $config.Password
BaseUrl = $config.BaseUrl
}
$headers = Get-AuthorizationHeaders @splatAuthHeaders
$splatRestMethod = @{
Uri = "$($Config.BaseUrl)/ExosApi/api/v1.0/persons/$($aRef)/block"
Method = 'Post'
Headers = $headers
body = @{ Reason = 'Automated HelloId Provisioing' } | ConvertTo-Json
}
try {
$null = Invoke-RestMethod @splatRestMethod -Verbose:$false
} catch {
if ($_.ErrorDetails.message -notmatch 'Person can not be blocked as it has already been blocked') {
throw $_
}
}
Write-Verbose 'Verify if the account has badges assigned.'
$splatGetPersons = @{
Uri = "$($Config.BaseUrl)/ExosApi/api/v1.0/persons?`$filter=(PersonBaseData/PersonId eq '$($Aref)')&`$expand=Badge(`$select=badgename)"
Method = 'GET'
Headers = $headers
}
$responseUser = (Invoke-RestMethod @splatGetPersons -Verbose:$false).value[0]
if ($responseUser.Badge.badgeName.count -eq 0) {
Write-Verbose "No Badges were assigned to [$($Aref)]"
} else {
foreach ($badge in $responseUser.Badge) {
Write-Verbose "Unassign Badge [$($badge.BadgeName)]"
$splatRestMethod = @{
Uri = "$($Config.BaseUrl)/ExosApi/api/v1.0/persons/$($aRef)/unassignBadge" # Odata Query
Method = 'Post'
Headers = $headers
body = @{ BadgeName = $badge.BadgeName } | ConvertTo-Json
}
$null = Invoke-RestMethod @splatRestMethod -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Message = "Unassign Badge [$($badge.BadgeName)] was successful"
IsError = $false
})
}
}
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = 'Disable account was successful'
IsError = $false
})
}
} catch {
$errorMessage = "Could not update DormakabaExos account. Error: $($_.Exception.Message) $($_.ErrorDetails.message)"
Write-Verbose $errorMessage -Verbose
$auditLogs.Add([PSCustomObject]@{
Message = $errorMessage
IsError = $true
})
} finally {
if ($null -ne $headers) {
Write-Verbose 'logout'
$splatLogOut = @{
Uri = "$($Config.BaseUrl)/ExosApi/api/v1.0/logins/logoutMyself"
Method = 'POST'
Headers = $headers
}
try {
$null = Invoke-RestMethod @splatLogOut -Verbose:$false
} catch {
Write-Verbose "Warning LogoutMyself Failed, $($_.Exception.Message) $($_.ErrorDetails.message)".trim(' ') -Verbose
}
}
$result = [PSCustomObject]@{
Success = $success
Auditlogs = $auditLogs
}
Write-Output $result | ConvertTo-Json -Depth 10
}