-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.ps1
502 lines (437 loc) · 20.1 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#################################################
# HelloID-Conn-Prov-Target-Inception-Update
# PowerShell V2
# Version: 2.0.0
#################################################
# Set to false at start, because only when no error occurs it is set to true
$outputContext.Success = $false
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
# Set debug logging
switch ($($actionContext.Configuration.isDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
# Script Configuration
$departmentLookupProperty = { $_.Department.ExternalId }
$titleLookupProperty = { $_.Title.ExternalId }
#region functions
function Get-InceptionToken {
[CmdletBinding()]
param()
try {
$splatTokenParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/api/v2/authentication/login"
Method = 'POST'
Body = @{
username = $actionContext.Configuration.UserName
password = $actionContext.Configuration.Password
} | ConvertTo-Json
Headers = @{
Accept = 'application/json'
'Content-Type' = 'application/json'
}
}
$tokenResponse = Invoke-RestMethod @splatTokenParams -Verbose:$false
Write-Output $tokenResponse.Token
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
function Get-InceptionPosition {
[CmdletBinding()]
Param(
$pageSize = 200,
$headers
)
try {
$pageNumber = 1
$positions = [System.Collections.Generic.list[object]]::new()
do {
$splatUserParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/api/v2/hrm/positions?pagesize=$pageSize&page=$pageNumber"
Method = 'GET'
Headers = $headers
}
Write-Verbose "$($splatUserParams.Uri)"
$positionsResponse = Invoke-RestMethod @splatUserParams -Verbose:$false
if ($null -ne $positionsResponse.items) {
$positions.AddRange($positionsResponse.items)
}
$pageNumber++
}until ( $positions.count -eq $positionsResponse.total )
Write-Output $positions
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
function Get-InceptionOrgunit {
[CmdletBinding()]
Param(
$pageSize = 200,
$headers
)
try {
$pageNumber = 1
$orgunits = [System.Collections.Generic.list[object]]::new()
do {
$splatUserParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/api/v2/hrm/orgunits?pagesize=$pageSize&page=$pageNumber"
Method = 'GET'
Headers = $headers
}
Write-Verbose "$($splatUserParams.Uri)"
$orgunitsResponse = Invoke-RestMethod @splatUserParams -Verbose:$false
if ($null -ne $orgunitsResponse.items) {
$orgunits.AddRange($orgunitsResponse.items)
}
$pageNumber++
}until ( $orgunits.count -eq $orgunitsResponse.total )
Write-Output $orgunits
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
function Get-InceptionIdsFromHelloIdContract {
[CmdletBinding()]
param(
[System.Object[]]
[Parameter()]
$DesiredContracts,
[Parameter(Mandatory)]
$LookupFieldOrgunitId,
[Parameter(Mandatory)]
$LookupFieldPositionId,
[System.Collections.Hashtable]
[Parameter(Mandatory)]
$MappingOrgUnits,
[System.Collections.Hashtable]
[Parameter(Mandatory)]
$MappingPositions
)
try {
if ($null -eq $desiredContracts) {
throw 'No contracts in condition'
}
Write-Verbose 'Calculate and validate the desired Positions and organization Units'
if ((($desiredContracts | Select-Object $LookupFieldOrgunitId).$LookupFieldOrgunitId | Measure-Object).count -ne $desiredContracts.count) {
throw "Not all desired contracts hold a value with Property [$LookupFieldOrgunitId]. Verify your source mapping."
}
if ((($desiredContracts | Select-Object $LookupFieldPositionId).$LookupFieldPositionId | Measure-Object).count -ne $desiredContracts.count) {
throw "Not all desired contracts hold a value with Property [$LookupFieldPositionId]. Verify your source mapping."
}
$desiredPositionList = [System.Collections.Generic.list[object]]::new()
$idOrgUnitNotFound = [System.Collections.Generic.list[string]]::new()
$idPositionNotFound = [System.Collections.Generic.list[string]]::new()
foreach ($contract in $DesiredContracts) {
$contractOrgUnitsValue = "$(($contract | Select-Object $LookupFieldOrgunitId )."$($LookupFieldOrgunitId)")"
$orgunitid = ($MappingOrgUnits["$($contractOrgUnitsValue)"]).id
if ( [string]::IsNullOrEmpty($orgunitid)) {
$idOrgUnitNotFound.Add($contractOrgUnitsValue)
}
$contractPositionValue = "$(($contract | Select-Object $LookupFieldPositionId)."$($LookupFieldPositionId)")"
$positionid = ($MappingPositions["$($contractPositionValue)"]).id
if ( [string]::IsNullOrEmpty($positionId)) {
$idPositionNotFound.Add($contractPositionValue)
}
if ($null -eq ($desiredPositionList | Where-Object { $_.positionid -eq $positionId -and $_.orgunitid -eq $orgunitid })) {
$objectToAdd = [pscustomobject]@{
orgunitid = $orgunitid
orgunitName = $contractOrgUnitsValue
positionid = $positionId
positionName = $contractPositionValue
}
$desiredPositionList.Add(($objectToAdd))
}
}
if ( $idOrgUnitNotFound.count -gt 0 -or $idPositionNotFound.count -gt 0) {
$errorMessage = 'Missing Inception object.'
if ($idOrgUnitNotFound.count -gt 0 ) {
$errorMessage += " Orgunit not found [$LookupFieldOrgunitId [$($idOrgUnitNotFound -join ', ')]]"
}
if (($idPositionNotFound.count -gt 0) ) {
$errorMessage += " Position not found [$LookupFieldPositionId [$($idPositionNotFound -join ', ')]]"
}
throw $errorMessage
}
if ($desiredPositionList.count -eq 0) {
throw 'No Position Found'
}
Write-Output $desiredPositionList
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
function Update-PositionsPerOrgUnitsList {
[CmdletBinding()]
param(
[System.Array]
[AllowNull()]
$AccountReference,
[System.Array]
[AllowNull()]
$DesiredPositions,
[System.Array]
$CurrentPositionsInInception,
$DryRunFlag
)
try {
if ($null -eq $AccountReference) {
$accountReference = [System.Collections.Generic.List[object]]::new()
}
if ($null -eq $DesiredPositions) {
$DesiredPositions = [System.Collections.Generic.List[object]]::new()
}
$compareResults = Compare-Object $AccountReference $DesiredPositions -Property positionid, orgunitid -PassThru
foreach ($compareResultItem in $compareResults) {
if ($compareResultItem.SideIndicator -eq '=>' ) {
$currentObject = ($compareResultItem | Select-Object * -ExcludeProperty SideIndicator)
Write-Verbose "Process Position [$($currentObject)] of Employee"
$itemToAdd = $CurrentPositionsInInception | Where-Object { $_.positionid -eq $compareResultItem.positionid -and $_.orgunitid -eq $compareResultItem.orgunitid }
if (-not ($CurrentPositionsInInception.Contains($itemToAdd))) {
[array]$CurrentPositionsInInception += ($currentObject)
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Added Position [OrgUnit: $($currentObject.OrgunitName) Position: $($currentObject.PositionName)]"
IsError = $false
})
if ($DryRunFlag -eq $true) {
Write-Warning "[DryRun] Added Position [OrgUnit: $($currentObject.orgunitid) Position: $($currentObject.positionid)]"
}
}
else {
if ($DryRunFlag -eq $true) {
Write-Warning "[DryRun] Calculated Position already exists [OrgUnit: $($currentObject.orgunitid) Position: $($currentObject.positionid)]"
}
}
}
elseif ($compareResultItem.SideIndicator -eq '<=' ) {
$itemToRemove = $CurrentPositionsInInception | Where-Object { $_.positionid -eq $compareResultItem.positionid -and $_.orgunitid -eq $compareResultItem.orgunitid }
if (-not [string]::IsNullOrEmpty($itemToRemove)) {
Write-Verbose "Removing Position [$($itemToRemove)] from Employee"
$CurrentPositionsInInception = $CurrentPositionsInInception.Where({ $_ -ne $itemToRemove })
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Removed Position [OrgUnit: $($compareResultItem.OrgunitName) Position: $($compareResultItem.PositionName)]"
IsError = $false
})
}
else {
if ($DryRunFlag -eq $true) {
Write-Warning "[DryRun] Previously assigned Position [$($compareResultItem | Select-Object * -ExcludeProperty SideIndicator)] is already removed from Employee"
}
}
}
}
Write-Output $CurrentPositionsInInception | Select-Object positionid, orgunitid
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
function Resolve-InceptionError {
[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
}
}
}
try {
$errorDetailsObject = ($httpErrorObj.ErrorDetails | ConvertFrom-Json)
# Make sure to inspect the error result object and add only the error message as a FriendlyMessage.
if (-not [string]::IsNullOrEmpty($errorDetailsObject.languageString)) {
$httpErrorObj.FriendlyMessage = $errorDetailsObject.LanguageString
}
elseif (-not [string]::IsNullOrEmpty($errorDetailsObject.description)) {
$httpErrorObj.FriendlyMessage = $errorDetailsObject.Description
}
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails # Temporarily assignment
}
catch {
$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'
}
Write-Verbose "Verifying if a Inception account for [$($personContext.Person.DisplayName)] exists"
$headers = @{
Accept = 'application/json'
Authorization = "Bearer $(Get-InceptionToken)"
}
try {
$splatUserParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/api/v2/hrm/employees/$($actionContext.References.Account.AccountReference)"
Method = 'GET'
Headers = $Headers
}
$correlatedAccount = Invoke-RestMethod @splatUserParams -Verbose:$false
}
catch {
if ( $_.Exception.message -notmatch '404' ) {
throw $_
}
}
# Remove supervisorid field when manager is unknown
if ($actionContext.Data.PSObject.Properties.Name -Contains 'supervisorid') {
if ($null -ne $actionContext.References.ManagerAccount.AccountReference) {
$actionContext.Data.supervisorid = $actionContext.References.ManagerAccount.AccountReference
}
else {
$actionContext.Data.PSObject.Properties.Remove('supervisorid')
}
}
$actionContext.Data.id = $actionContext.References.Account.AccountReference
$propertiesToCompare = $actionContext.Data.PSObject.Properties.Name
# Always compare the account against the current account in target system
if ($null -ne $correlatedAccount) {
$splatCompareProperties = @{
ReferenceObject = $correlatedAccount.PSObject.Properties | Where-Object { $_.Name -in $propertiesToCompare }
DifferenceObject = $actionContext.Data.PSObject.Properties | Where-Object { $_.Name -in $propertiesToCompare }
}
$propertiesChanged = Compare-Object @splatCompareProperties -PassThru | Where-Object { $_.SideIndicator -eq '=>' }
# Retrieve Metadata
[array]$desiredContracts = $personContext.Person.Contracts | Where-Object { $_.Context.InConditions -eq $true }
if ($actionContext.DryRun -eq $true) {
[array]$desiredContracts = $personContext.Person.Contracts
}
Write-Verbose 'Gathering Inception Positions and organization Units to map the against the HelloId person'
$positions = Get-InceptionPosition -Headers $headers
$orgUnits = Get-InceptionOrgunit -Headers $headers
$splatGetInceptionIds = @{
DesiredContracts = $desiredContracts
LookupFieldPositionId = $titleLookupProperty
LookupFieldOrgunitId = $departmentLookupProperty
MappingPositions = ($positions | Group-Object Code -AsHashTable -AsString)
MappingOrgUnits = ($orgUnits | Group-Object Code -AsHashTable -AsString)
}
$desiredPositionList = Get-InceptionIdsFromHelloIdContract @splatGetInceptionIds
$splatPositionsPerOrgUnitsList = @{
DesiredPositions = $desiredPositionList
CurrentPositionsInInception = $actionContext.Data.positionsPerOrgUnits
}
# Update function also writes auditlogs and dryrun logging
$positionsPerOrgUnits = ([array](Update-PositionsPerOrgUnitsList @splatPositionsPerOrgUnitsList -DryRunFlag:$actionContext.DryRun))
if ($null -ne (Compare-Object $positionsPerOrgUnits $correlatedAccount.positionsPerOrgUnits -Property positionid, orgunitid)) {
Write-Verbose 'Position update required'
$positionsAction = 'Update'
}
else {
$positionsAction = 'NoChanges'
}
if (($propertiesChanged -or $positionsAction -eq 'Update') -and ($null -ne $correlatedAccount)) {
$action = 'UpdateAccount'
$dryRunMessage = "Account property(s) required to update: $($propertiesChanged.Name -join ', ')"
}
elseif (-not($propertiesChanged) -and ($null -ne $correlatedAccount)) {
$action = 'NoChanges'
$dryRunMessage = 'No changes will be made to the account during enforcement'
}
}
else {
$action = 'NotFound'
$dryRunMessage = "Inception account for: [$($personContext.Person.DisplayName)] not found. Possibly deleted."
}
# Add a message and the result of each of the validations showing what will happen during enforcement
if ($actionContext.DryRun -eq $true) {
Write-Verbose "[DryRun] $dryRunMessage"
}
# Process
if (-not($actionContext.DryRun -eq $true)) {
switch ($action) {
'UpdateAccount' {
Write-Verbose "Updating Inception account with accountReference: [$($actionContext.References.Account.AccountReference)]"
$body = @{}
if ($propertiesChanged) {
foreach ($prop in $propertiesChanged) {
$body["$($prop.name)"] = $prop.value
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "'Account property(s) required to update: [$($propertiesChanged.name -join ',')]'"
IsError = $false
})
}
if ($positionsAction -eq 'Update') {
$body['positionsPerOrgUnits'] = $positionsPerOrgUnits
}
$splatEmployeeUpdateParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/api/v2/hrm/employees/$($actionContext.References.Account.AccountReference)"
Method = 'PUT'
Headers = $headers
Body = ($body | ConvertTo-Json)
ContentType = 'application/json; charset=utf-8'
}
$null = Invoke-RestMethod @splatEmployeeUpdateParams -Verbose:$false
$outputContext.AccountReference.Positions = $positionsPerOrgUnits
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Update account was successful, Account property(s) updated: [$($propertiesChanged.name -join ',')]"
IsError = $false
})
break
}
'NoChanges' {
Write-Verbose "No changes to Inception account with accountReference: [$($actionContext.References.Account)]"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = 'No changes will be made to the account during enforcement'
IsError = $false
})
break
}
'NotFound' {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Inception account for: [$($personContext.Person.DisplayName)] could not be found, possibly indicating that it could be deleted, or the account is not correlated"
IsError = $true
})
break
}
}
}
}
catch {
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-InceptionError -ErrorObject $ex
$auditMessage = "Could not update Inception account. Error: $($errorObj.FriendlyMessage)"
Write-Warning "Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
}
else {
$auditMessage = "Could not update Inception account. Error: $($ex.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
})
}
finally {
# Check if auditLogs contains errors, if no errors are found, set success to true
if (-not($outputContext.AuditLogs.IsError -contains $true)) {
$outputContext.Success = $true
}
}