-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ps1
175 lines (161 loc) · 6 KB
/
update.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
#####################################################
# HelloID-Conn-Prov-Target-DormakabaExos-Update
#
# Version: 1.0.0
#####################################################
# Initialize default values
$config = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json
$pp = $previousPerson | ConvertFrom-Json
$aRef = $AccountReference | ConvertFrom-Json
$success = $false
$auditLogs = [System.Collections.Generic.List[PSCustomObject]]::new()
# Account mapping
$account = [PSCustomObject]@{
PersonBaseData = [PSCustomObject]@{
PersonalNumber = $p.ExternalId
FirstName = $p.Name.GivenName
LastName = "$($p.Name.FamilyNamePrefix) $($p.Name.FamilyName)".trim(' ')
EMail = $p.Accounts.MicrosoftActiveDirectory.mail
}
}
$previousAccount = [PSCustomObject]@{
PersonBaseData = [PSCustomObject]@{
PersonalNumber = $pp.ExternalId
FirstName = $pp.Name.GivenName
LastName = "$($pp.Name.FamilyNamePrefix) $($pp.Name.FamilyName)".trim(' ')
EMail = $pp.Accounts.MicrosoftActiveDirectory.mail
}
}
# 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
try {
# Verify if the account must be updated
$splatCompareProperties = @{
ReferenceObject = @($previousAccount.PersonBaseData.PSObject.Properties)
DifferenceObject = @($account.PersonBaseData.PSObject.Properties)
}
$propertiesChanged = (Compare-Object @splatCompareProperties -PassThru).Where({ $_.SideIndicator -eq '=>' })
if ($propertiesChanged) {
$action = 'Update'
} else {
$action = 'NoChanges'
}
# Add an auditMessage showing what will happen during enforcement
if ($dryRun -eq $true) {
$auditLogs.Add([PSCustomObject]@{
Message = "Update DormakabaExos account for: [$($p.DisplayName)] will be executed during enforcement"
})
}
if (-not($dryRun -eq $true)) {
switch ($action) {
'Update' {
Write-Verbose "Updating DormakabaExos account with accountReference: [$aRef]"
$splatAuthHeaders = @{
Username = $config.UserName
Password = $config.Password
BaseUrl = $config.BaseUrl
}
$headers = Get-AuthorizationHeaders @splatAuthHeaders
$body = @{
PersonBaseData = @{}
}
foreach ($property in $propertiesChanged) {
$body.PersonBaseData["$($property.name)"] = $property.value
}
$splatRestMethod = @{
Uri = "$($Config.BaseUrl)/ExosApi/api/v1.0/persons/$($aRef)/Update"
Method = 'Post'
Headers = $headers
body = ($body | ConvertTo-Json -Depth 10)
}
$null = Invoke-RestMethod @splatRestMethod -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Message = 'Update account was successful'
IsError = $false
})
break
}
'NoChanges' {
Write-Verbose "No changes to DormakabaExos account with accountReference: [$aRef]"
$auditLogs.Add([PSCustomObject]@{
Message = 'Update skipped. No change is required to the DormakabaExos account.'
IsError = $false
})
break
}
}
$success = $true
}
} 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
Account = $account
Auditlogs = $auditLogs
}
Write-Output $result | ConvertTo-Json -Depth 10
}