-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ps1
197 lines (171 loc) · 7.49 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
#####################################################
# HelloID-Conn-Prov-Target-AFAS-Profit-Employees-Create
# PowerShell V2
#####################################################
# Set to true at start, because only when an error occurs it is set to false
$outputContext.Success = $true
# 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
# Get current account and verify if there are changes
try {
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 {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Configuration of correlation is mandatory."
IsError = $true
})
Throw "Configuration of correlation is mandatory."
}
Write-Verbose "Querying AFAS employee 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.Medewerker) {
throw "No AFAS employee found AFAS employee where [$($correlationProperty)] = [$($correlationValue)]"
}
if (-Not($actionContext.DryRun -eq $true)) {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "CorrelateAccount"
Message = "Correlated account [$($currentAccount.Medewerker)] on field [$($correlationProperty)] with value [$($correlationValue)]"
IsError = $false
})
$aRef = [PSCustomObject]@{
Medewerker = $currentAccount.Medewerker
Persoonsnummer = $currentAccount.Persoonsnummer
}
$outputContext.AccountCorrelated = $true
$outputContext.AccountReference = $aRef
}
else {
Write-Warning "DryRun: Would correlate AFAS employee [$($currentAccount.Medewerker)]."
}
}
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 = "CorrelateAccount"
Message = "Error querying AFAS employee where [$($correlationProperty)] = [$($correlationValue)]. Error Message: $($errorMessage.AuditErrorMessage)"
IsError = $true
})
}
finally {
# Check if auditLogs contains errors, if errors are found, set succes to false
if ($outputContext.AuditLogs.IsError -contains $true) {
$outputContext.Success = $false
}
}