-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersons.ps1
195 lines (161 loc) · 9.35 KB
/
persons.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
#####################################################
# HelloID-Conn-Prov-Source-Magister-Students
#
# Version: 2.0.0
#####################################################
# 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
$VerbosePreference = "SilentlyContinue"
$InformationPreference = "Continue"
$WarningPreference = "Continue"
$c = $configuration | ConvertFrom-Json
$baseUri = $c.BaseUrl
$username = $c.Username
$password = $c.Password
$layout = $c.Layout
$uri = "$baseUri/doc?Function=GetData&Library=Data&SessionToken=$username%3B$password&Layout=$layout&Parameters=&Type=CSV&Encoding=ANSI"
# Set debug logging
switch ($($c.isDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
Write-Information "Start person import: Base URL: [$baseUri], Using username: $username"
#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
}
}
#endregion functions
#region query persons
try {
Write-Verbose "Querying Persons"
$result = Invoke-WebRequest -Method GET -Uri $uri -UseBasicParsing
$data = $result.content
$persons = ConvertFrom-Csv $data -Delimiter ";"
Write-Information "Succesfully queried Persons. Result count: $($persons.count)"
# test single student:
# $persons = $persons | where-object stamnr -eq '<stamnr student>'
# Write-verbose -verbose "$($persons | convertto-json)"
if ($persons.Count -eq 0) {
throw "Empty Persons data, aborting..."
}
}
catch {
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObject = Resolve-HTTPError -Error $ex
$verboseErrorMessage = $errorObject.ErrorMessage
$auditErrorMessage = $errorObject.ErrorMessage
}
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($verboseErrorMessage)) {
$verboseErrorMessage = $ex.Exception.Message
}
if ([String]::IsNullOrEmpty($auditErrorMessage)) {
$auditErrorMessage = $ex.Exception.Message
}
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
throw "Could not query Persons. Error: $auditErrorMessage"
}
#endregion query persons
#region query enhancing and exporting person
try {
Write-Verbose 'Enhancing and exporting person objects to HelloID'
$employments = $persons | Group-Object Stamnr -AsHashTable
$persons | Add-Member -MemberType NoteProperty -Name "ExternalId" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "NamingConvention" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "PersonType" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "Contracts" -Value $null -Force
$persons | ForEach-Object {
$_.ExternalId = $_.Stamnr
$_.DisplayName = "$($_.FIRSTNAME) $($_.PREFIX) $($_.LASTNAME) ($($_.Stamnr))" -replace " ", " "
$_.NamingConvention = "B"
$_.PersonType = "Leerling"
$contracts = [System.Collections.ArrayList]::new()
$personEmployments = $employments[$_.Stamnr]
foreach ($personEmployment in $personEmployments) {
# Compose contract start/enddate from the study enrollment
if (![string]::IsNullOrEmpty($personEmployment.STUDYPERIOD)) {
# Select Mentor with preference for CMentor
if ([string]::IsNullOrEmpty($personEmployment.CMENTORCODE)) {
$mentorCode = $personEmployment.PMENTORCODE
$mentorName = $personEmployment.PMENTORNAME
}
else {
$mentorCode = $personEmployment.CMENTORCODE
$mentorName = $personEmployment.CMENTORNAME
}
$contract = [PSCustomObject]@{
ExternalId = "$($personEmployment.Stamnr)_$($personEmployment.STUDYPERIOD)_$($personEmployment.CLASS)_$($personEmployment.PROFILECODE)_$($personEmployment.studiebegindatum)"
StartDate = $personEmployment.studiebegindatum
EndDate = $personEmployment.studieeinddatum
Class = $personEmployment.CLASS
ProfileCode = $personEmployment.PROFILECODE
ProfileDescription = $personEmployment.PROFILEDESC
Study = $personEmployment.STUDY
Location = $personEmployment.LOCATION
Year = $personEmployment.STUDYYEAR
StudyPeriod = $personEmployment.STUDYPERIOD
MentorCode = $mentorCode
MentorName = $mentorName
}
[void]$contracts.Add($contract)
}
else {
write-warning "Stamnr $($_.Stamnr) has no STUDYPERIOD"
}
}
$_.Contracts = $contracts
}
$persons = $persons | Select-Object -Property ExternalId, DisplayName, NamingConvention, Stamnr, INITIALS, FIRSTNAME, PREFIX, LASTNAME, Email, GENDER, BIRTHDATE, STREET, HOUSENR, HOUSENRSUFIX, POSTALCODE, CITY, Loginaccount.Naam, email_1, Mobiel_nr, Contracts
# Skip the first line containing the column headers
$persons = $persons | Where-Object { $_.ExternalId -notlike "*Stamnr" }
# Remove duplicate persons
$persons = $persons | Sort-Object ExternalId -Unique
# Sanitize and export the json
$output = $persons | ConvertTo-Json -Depth 10
$output = $output.Replace("Loginaccount.Naam", "Loginaccount_Naam")
Write-Output $output
Write-Information "Succesfully enhanced and exported person objects to HelloID. Result count: $($persons.count)"
Write-Information "Person import completed"
}
catch {
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObject = Resolve-HTTPError -Error $ex
$verboseErrorMessage = $errorObject.ErrorMessage
$auditErrorMessage = $errorObject.ErrorMessage
}
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($verboseErrorMessage)) {
$verboseErrorMessage = $ex.Exception.Message
}
if ([String]::IsNullOrEmpty($auditErrorMessage)) {
$auditErrorMessage = $ex.Exception.Message
}
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
throw "Could not enhance and export person objects to HelloID. Error: $auditErrorMessage"
}
#endregion query enhancing and exporting person