-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ps1
304 lines (263 loc) · 12.2 KB
/
create.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
#####################################################
# HelloID-Conn-Prov-Target-AFAS-Profit-Users-Create
# PowerShell V2
#####################################################
# Set to false at start, at the end, only when no error occurs it is set to true
$outputContext.Success = $false
# AccountReference must have a value for dryRun
$outputContext.AccountReference = "Unknown"
# Set debug logging
switch ($($actionContext.Configuration.isDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#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 Resolve-AFASErrorMessage {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
try {
$errorObjectConverted = $ErrorObject | ConvertFrom-Json -ErrorAction Stop
if ($null -ne $errorObjectConverted.externalMessage) {
$errorMessage = $errorObjectConverted.externalMessage
}
else {
$errorMessage = $errorObjectConverted
}
}
catch {
$errorMessage = "$($ErrorObject.Exception.Message)"
}
Write-Output $errorMessage
}
}
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 -ErrorObject $ErrorObject
if (-not[String]::IsNullOrEmpty($httpErrorObject.ErrorMessage)) {
$errorMessage.VerboseErrorMessage = $httpErrorObject.ErrorMessage
$errorMessage.AuditErrorMessage = Resolve-AFASErrorMessage -ErrorObject $httpErrorObject.ErrorMessage
}
else {
$errorMessage.VerboseErrorMessage = $ErrorObject.Exception.Message
$errorMessage.AuditErrorMessage = $ErrorObject.Exception.Message
}
}
# 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
}
}
#endregion functions
try {
$account = $actionContext.Data
$exportData = $account.PsObject.Copy()
# Remove field because only used for export data or to set correlation
if ($account.PSObject.Properties.Name -Contains 'Gebruiker') {
$account.PSObject.Properties.Remove('Gebruiker')
}
if ($account.PSObject.Properties.Name -Contains 'Medewerker') {
$account.PSObject.Properties.Remove('Medewerker')
}
if ($actionContext.CorrelationConfiguration.Enabled) {
$correlationProperty = $actionContext.CorrelationConfiguration.accountField
$correlationValue = $actionContext.CorrelationConfiguration.accountFieldValue
if ([string]::IsNullOrEmpty($correlationProperty)) {
Write-Warning "Correlation is enabled but not configured correctly."
throw "Correlation is enabled but not configured correctly."
}
if ([string]::IsNullOrEmpty($correlationValue)) {
Write-Warning "The correlation value for [$correlationProperty] is empty. This is likely a scripting issue."
throw "The correlation value for [$correlationProperty] is empty. This is likely a scripting issue."
}
}
else {
Write-Warning "Correlation is enabled but not configured correctly."
throw "Configuration of correlation is mandatory."
}
# Get current account and verify if the action should be either [updated and correlated] or just [correlated]
try {
Write-Verbose "Querying AFAS user where [$($correlationProperty)] = [$($correlationValue)]"
# Create authorization headers
$encodedToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($($actionContext.Configuration.Token)))
$authValue = "AfasToken $encodedToken"
$Headers = @{ Authorization = $authValue }
$Headers.Add("IntegrationId", "45963_140664") # Fixed value - Tools4ever Partner Integration ID
$splatWebRequest = @{
Uri = "$($actionContext.Configuration.BaseUri)/connectors/$($actionContext.Configuration.GetConnector)?filterfieldids=$($correlationProperty)&filtervalues=$($correlationValue)&operatortypes=1"
Headers = $headers
Method = 'GET'
ContentType = "application/json;charset=utf-8"
UseBasicParsing = $true
}
$currentAccount = (Invoke-RestMethod @splatWebRequest -Verbose:$false).rows
if ($null -eq $currentAccount.Gebruiker) {
if ($($actionContext.Configuration.createUser) -eq $true) {
Write-Verbose "No AFAS user found where [$($correlationProperty)] = [$($correlationValue)]. Creating new user"
}
else {
throw "No AFAS user found where [$($correlationProperty)] = [$($correlationValue)] and [create user when not found in AFAS] set to [$($actionContext.Configuration.createUser)]"
}
}
else {
$aRef = [PSCustomObject]@{
Gebruiker = $currentAccount.Gebruiker
}
$outputContext.AccountReference = $aRef
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "CorrelateAccount"
Message = "Successfully correlated to AFAS user [$($currentAccount.Gebruiker)]"
IsError = $false
})
$outputContext.AccountCorrelated = $true
}
}
catch {
$ex = $PSItem
$errorMessage = Get-ErrorMessage -ErrorObject $ex
Write-Verbose "Error at Line [$($ex.InvocationInfo.ScriptLineNumber)]: $($ex.InvocationInfo.Line). Error: $($errorMessage.VerboseErrorMessage)"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error querying AFAS user where [$($correlationProperty)] = [$($correlationValue)]. Error Message: $($errorMessage.AuditErrorMessage)"
IsError = $true
})
# Skip further actions, as this is a critical error
throw "Error querying AFAS user"
}
# Only create
if (!$outputContext.AccountCorrelated) {
# Create AFAS User
try {
# Create custom account object for create and set with default properties and values
$createAccount = [PSCustomObject]@{
'KnUser' = @{
'Element' = @{
# Gebruiker
'@UsId' = $account.UsId
'Fields' = @{
# Nummer
'BcCo' = $currentAccount.Persoonsnummer
}
}
}
}
# Add all account properties to the custom account object for create - Except for UsId as it is set at a different level
foreach ($accountProperty in $account.PSObject.Properties | Where-Object { $_.Name -ne 'UsId' }) {
$createAccount.KnUser.Element.Fields.$($accountProperty.Name) = $accountProperty.Value
}
$body = ($createAccount | ConvertTo-Json -Depth 10)
$splatWebRequest = @{
Uri = "$($actionContext.Configuration.BaseUri)/connectors/$($actionContext.Configuration.UpdateConnector)"
Headers = $headers
Method = 'PUT'
Body = ([System.Text.Encoding]::UTF8.GetBytes($body))
ContentType = "application/json;charset=utf-8"
UseBasicParsing = $true
}
if (-Not($actionContext.DryRun -eq $true)) {
Write-Verbose "Creating AFAS user [$($account.UsId)]. Account object: $($createAccount | ConvertTo-Json -Depth 10)"
$createdAccount = Invoke-RestMethod @splatWebRequest -Verbose:$false
# Set aRef object for use in futher actions
$aRef = [PSCustomObject]@{
Gebruiker = $account.UsId
}
$outputContext.AccountReference = $aRef
# Add correlation property to exportdata
$exportData | Add-Member -MemberType NoteProperty -Name $correlationProperty -Value $correlationValue -Force
# Add aRef properties to exportdata
foreach ($aRefProperty in $aRef.PSObject.Properties) {
$exportData | Add-Member -MemberType NoteProperty -Name $aRefProperty.Name -Value $aRefProperty.Value -Force
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully created AFAS user [$($account.UsId)]"
IsError = $false
})
}
else {
Write-Warning "DryRun: Would create AFAS user [$($account.UsId)]. Account object: $($createAccount | ConvertTo-Json -Depth 10)"
}
}
catch {
$ex = $PSItem
$errorMessage = Get-ErrorMessage -ErrorObject $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($errorMessage.VerboseErrorMessage)"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error creating AFAS user [$($account.UsId)]. Error Message: $($errorMessage.AuditErrorMessage)."
IsError = $true
})
# Skip further actions, as this is a critical error
throw "Error creating AFAS user"
}
}
}
catch {
$ex = $PSItem
if ((-Not($ex.Exception.Message -eq 'Error querying AFAS user')) -and (-Not($ex.Exception.Message -eq 'Error creating AFAS user'))) {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "$($ex.Exception.Message)"
IsError = $true
})
}
else {
Write-Verbose "ERROR: $ex"
}
}
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
}
$outputContext.Data = $exportData
}