-
Notifications
You must be signed in to change notification settings - Fork 0
/
departments.ps1
355 lines (291 loc) · 13.8 KB
/
departments.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
#####################################################
# HelloID-Conn-Prov-Source-RAET-IAM-API-Beaufort-Departments
#
# Version: 2.2.0
#####################################################
$c = $configuration | ConvertFrom-Json
# Set debug logging
switch ($($c.isDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
$InformationPreference = "Continue"
$WarningPreference = "Continue"
# 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
$clientId = $c.clientId
$clientSecret = $c.clientSecret
$tenantId = $c.tenantId
$Script:AuthenticationUri = "https://connect.visma.com/connect/token"
$Script:BaseUri = "https://api.youforce.com"
#region functions
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 Get-ErrorMessage {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
$errorMessage = [PSCustomObject]@{
VerboseErrorMessage = $null
AuditErrorMessage = $null
}
if ( $($ErrorObject.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$httpErrorObject = Resolve-HTTPError -Error $ErrorObject
$errorMessage.VerboseErrorMessage = $httpErrorObject.ErrorMessage
$errorMessage.AuditErrorMessage = $httpErrorObject.ErrorMessage
}
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($errorMessage.VerboseErrorMessage)) {
$errorMessage.VerboseErrorMessage = $ErrorObject.Exception.Message
}
if ([String]::IsNullOrEmpty($errorMessage.AuditErrorMessage)) {
$errorMessage.AuditErrorMessage = $ErrorObject.Exception.Message
}
Write-Output $errorMessage
}
}
function New-RaetSession {
[CmdletBinding()]
param (
[Alias("Param1")]
[parameter(Mandatory = $true)]
[string]
$ClientId,
[Alias("Param2")]
[parameter(Mandatory = $true)]
[string]
$ClientSecret,
[Alias("Param3")]
[parameter(Mandatory = $false)]
[string]
$TenantId
)
#Check if the current token is still valid
$accessTokenValid = Confirm-AccessTokenIsValid
if ($true -eq $accessTokenValid) {
return
}
try {
# 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
$authorisationBody = @{
'grant_type' = "client_credentials"
'client_id' = $ClientId
'client_secret' = $ClientSecret
'tenant_id' = $TenantId
}
$splatAccessTokenParams = @{
Uri = $Script:AuthenticationUri
Headers = @{'Cache-Control' = "no-cache" }
Method = 'POST'
ContentType = "application/x-www-form-urlencoded"
Body = $authorisationBody
UseBasicParsing = $true
}
Write-Verbose "Creating Access Token at uri '$($splatAccessTokenParams.Uri)'"
$result = Invoke-RestMethod @splatAccessTokenParams -Verbose:$false
if ($null -eq $result.access_token) {
throw $result
}
$Script:expirationTimeAccessToken = (Get-Date).AddSeconds($result.expires_in)
$Script:AuthenticationHeaders = @{
'Authorization' = "Bearer $($result.access_token)"
'Accept' = "application/json"
}
Write-Verbose "Successfully created Access Token at uri '$($splatAccessTokenParams.Uri)'"
}
catch {
$ex = $PSItem
$errorMessage = Get-ErrorMessage -ErrorObject $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($($errorMessage.VerboseErrorMessage))"
$auditLogs.Add([PSCustomObject]@{
# Action = "" # Optional
Message = "Error creating Access Token at uri ''$($splatAccessTokenParams.Uri)'. Please check credentials. Error Message: $($errorMessage.AuditErrorMessage)"
IsError = $true
})
}
}
function Confirm-AccessTokenIsValid {
if ($null -ne $Script:expirationTimeAccessToken) {
if ((Get-Date) -le $Script:expirationTimeAccessToken) {
return $true
}
}
return $false
}
function Invoke-RaetWebRequestList {
[CmdletBinding()]
param (
[parameter(Mandatory = $true)]
[string]
$Url
)
# 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
[System.Collections.ArrayList]$ReturnValue = @()
$counter = 0
$triesCounter = 0
do {
try {
$accessTokenValid = Confirm-AccessTokenIsValid
if ($true -ne $accessTokenValid) {
New-RaetSession -ClientId $clientId -ClientSecret $clientSecret -TenantId $tenantId
}
$retry = $false
if ($counter -gt 0 -and $null -ne $result.nextLink) {
$SkipTakeUrl = $result.nextLink.Substring($result.nextLink.IndexOf("?"))
}
else {
$SkipTakeUrl = "?take=1000"
}
$counter++
$splatGetDataParams = @{
Uri = "$Url$SkipTakeUrl"
Headers = $Script:AuthenticationHeaders
Method = 'GET'
ContentType = "application/json"
UseBasicParsing = $true
}
Write-Verbose "Querying data from '$($splatGetDataParams.Uri)'"
$result = Invoke-RestMethod @splatGetDataParams
# Check both the keys "values" and "value", since Extensions endpoint returns the data in "values" instead of "value"
if ($result.values.Count -ne 0) {
$resultObjects = $result.values
}
else {
$resultObjects = $result.value
}
# Check if resultObjects are an array if so, add the entire range, otherwise add the single object
if ($resultObjects -is [array]) {
[void]$ReturnValue.AddRange($resultObjects)
}
else {
[void]$ReturnValue.Add($resultObjects)
}
# Wait for 0,601 seconds - RAET IAM API allows a maximum of 100 requests a minute (https://community.visma.com/t5/Kennisbank-Youforce-API/API-Status-amp-Policy/ta-p/428099#toc-hId-339419904:~:text=3-,Spike%20arrest%20policy%20(max%20number%20of%20API%20calls%20per%20minute),100%20calls%20per%20minute,-*For%20the%20base).
Start-Sleep -Milliseconds 601
}
catch {
$ex = $PSItem
$errorMessage = Get-ErrorMessage -ErrorObject $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($errorMessage.VerboseErrorMessage)"
$maxTries = 3
if ( ($($errorMessage.AuditErrorMessage) -Like "*Too Many Requests*" -or $($errorMessage.AuditErrorMessage) -Like "*Connection timed out*") -and $triesCounter -lt $maxTries ) {
$triesCounter++
$retry = $true
$delay = 601 # Wait for 0,601 seconds - RAET IAM API allows a maximum of 100 requests a minute (https://community.visma.com/t5/Kennisbank-Youforce-API/API-Status-amp-Policy/ta-p/428099#toc-hId-339419904:~:text=3-,Spike%20arrest%20policy%20(max%20number%20of%20API%20calls%20per%20minute),100%20calls%20per%20minute,-*For%20the%20base).
Write-Warning "Error querying data from '$($splatGetDataParams.Uri)'. Error Message: $($errorMessage.AuditErrorMessage). Trying again in '$delay' milliseconds for a maximum of '$maxTries' tries."
Start-Sleep -Milliseconds $delay
}
else {
$retry = $false
throw "Error querying data from '$($splatGetDataParams.Uri)'. Error Message: $($errorMessage.AuditErrorMessage)"
}
}
}while (-NOT[string]::IsNullOrEmpty($result.nextLink) -or $retry -eq $true)
Write-Verbose "Successfully queried data from '$($Url)'. Result count: $($ReturnValue.Count)"
return $ReturnValue
}
#endregion functions
Write-Information "Starting department import. Base URL: $BaseUrl"
# Query organizationUnits
try {
Write-Verbose "Querying organizationUnits"
$organizationUnits = Invoke-RaetWebRequestList -Url "$BaseUri/iam/v1.0/organizationUnits"
Write-Information "Successfully queried organizationUnits. Result: $($organizationUnits.Count)"
}
catch {
$ex = $PSItem
$errorMessage = Get-ErrorMessage -ErrorObject $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($errorMessage.VerboseErrorMessage)"
throw "Error querying organizationUnits. Error Message: $($errorMessage.AuditErrorMessage)"
}
# Query roleAssignments
try {
Write-Verbose "Querying roleAssignments"
$roleAssignments = Invoke-RaetWebRequestList -Url "$BaseUri/iam/v1.0/roleAssignments"
# Sort Role assignments on personCode to make sure we always have the same manager with the same data
$roleAssignments = $roleAssignments | Sort-Object -Property { [int]$_.personCode }
Write-Information "Successfully queried roleAssignments. Result: $($roleAssignments.Count)"
}
catch {
$ex = $PSItem
$errorMessage = Get-ErrorMessage -ErrorObject $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($errorMessage.VerboseErrorMessage)"
throw "Error querying roleAssignments. Error Message: $($errorMessage.AuditErrorMessage)"
}
try {
Write-Verbose 'Enhancing and exporting department objects to HelloID'
# Set counter to keep track of actual exported person objects
$exportedDepartments = 0
$managerActiveCompareDate = Get-Date
foreach ($organizationUnit in $organizationUnits) {
$ouRoleAssignments = $roleAssignments | Where-Object { $_.organizationUnit -eq $organizationUnit.id }
# Sort role assignments on person code to make sure the order is always the same (if the data is the same)
$ouRoleAssignments = $ouRoleAssignments | Sort-Object personCode
# Organizational units may contain multiple managers (per organizational unit). There's no way to specify which manager is primary
# We check of the manager assignment is active and then select the first one we come across that's valid
$managerId = $null
foreach ($roleAssignment in $ouRoleAssignments) {
if (![string]::IsNullOrEmpty($roleAssignment)) {
if ($roleAssignment.ShortName -eq 'MGR') {
$startDate = ([Datetime]::ParseExact($roleAssignment.startDate, 'yyyy-MM-dd', $null))
$endDate = ([Datetime]::ParseExact($roleAssignment.endDate, 'yyyy-MM-dd', $null))
if ($startDate -lt $managerActiveCompareDate -and $endDate -ge $managerActiveCompareDate ) {
$managerId = $roleAssignment.personCode
break
}
}
}
}
$department = [PSCustomObject]@{
ExternalId = $organizationUnit.id
ShortName = $organizationUnit.shortName
DisplayName = $organizationUnit.fullName
ManagerExternalId = $managerId
ParentExternalId = $organizationUnit.parentOrgUnit
}
# Sanitize and export the json
$department = $department | ConvertTo-Json -Depth 10
$department = $department.Replace("._", "__")
Write-Output $department
# Updated counter to keep track of actual exported person objects
$exportedDepartments++
}
Write-Information "Successfully enhanced and exported department objects to HelloID. Result count: $($exportedDepartments)"
Write-Information "Department import completed"
}
catch {
$ex = $PSItem
$errorMessage = Get-ErrorMessage -ErrorObject $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($errorMessage.VerboseErrorMessage)"
throw "Could not enhance and export department objects to HelloID. Error Message: $($errorMessage.AuditErrorMessage)"
}