forked from Tools4everBV/HelloID-Conn-Prov-Target-Topdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrant_incident_permission.ps1
475 lines (444 loc) · 23.9 KB
/
grant_incident_permission.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
#Initialize default properties
$success = $False
#Write-Verbose -Verbose $person
$p = $person | ConvertFrom-Json;
$aRef = $accountReference | ConvertFrom-Json;
$pRef = $permissionReference | ConvertFrom-json;
$config = $configuration | ConvertFrom-Json
$auditMessage = " not created succesfully";
#TOPdesk system data
$url = $config.connection.url
$apiKey = $config.connection.apikey
$userName = $config.connection.username
$path = $config.notifications.jsonPath
# Enable TLS 1.2
if ([Net.ServicePointManager]::SecurityProtocol -notmatch "Tls12") {
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
}
function New-TopdeskIncident{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$incidentObject
)
$contentType = "application/json"
$bytes = [System.Text.Encoding]::ASCII.GetBytes("${userName}:${apiKey}")
$base64 = [System.Convert]::ToBase64String($bytes)
$headers = @{ Authorization = "BASIC $base64"; Accept = 'application/json'; "Content-Type" = 'application/json; charset=utf-8' }
$uriBranch = $url + "/branches"
$uriIncidents = $url + "/incidents"
$uriImpacts = $url + "/incidents/impacts"
$uriOperatorGroups = $url + "/operatorgroups?name=$($incidentObject.OperatorGroup)"
$uriProcessingStatus = $url + "/incidents/processing_status"
$uriPriorities = $url + "/incidents/priorities"
$uriCategories = $url + "/incidents/categories"
$uriSubCategories = $url + "/incidents/subcategories"
$uriEntryTypes = $url + "/incidents/entry_types"
$uriCallTypes = $url + "/incidents/call_types"
$uriCallerEmail = $url + "/persons?email=$($incidentObject.CallerEmail)"
$uriUrgencies = $url + "/incidents/urgencies"
if($incidentObject.Branch){
Write-Verbose -Verbose "Branch is specified. Checking whether the branch exists"
try{
$responseBranches = Invoke-WebRequest -Uri $uriBranch -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$branchAssign = $responseBranches | Where-Object{$_.name -eq $incidentObject.Branch}
if($null -eq $branchAssign){
Write-Verbose -Verbose "Branch '$($incidentObject.Branch)' not found, branch is ignored"
}elseif("2" -le $branchAssign.count){
Write-Verbose -Verbose "Multiple Branches '$($incidentObject.Branch)' found, branch is ignored"
}else{
$requestObject += @{
branch = @{
id = $branchAssign.id
}
}
Write-Verbose -Verbose "Added branch '$($branchAssign.name)' to the request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get branches, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get branches, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.OperatorGroup){
Write-Verbose -Verbose "Getting operator groups"
try{
$responseOperatorGroups = Invoke-WebRequest -Uri $uriOperatorGroups -Method Get -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$operatorGroupAssign = $responseOperatorGroups | Where-Object{$_.groupName -eq $incidentObject.OperatorGroup}
if($null -eq $operatorGroupAssign){
Write-Verbose -Verbose "Operator group '$($incidentObject.OperatorGroup)' not found, operator group is ignored"
}elseif("2" -le $operatorGroupAssign.count){
Write-Verbose -Verbose "Multiple operator groups '$($incidentObject.OperatorGroup)' found, operator group is ignored"
}else{
$requestObject += @{
operatorGroup = @{
id = $operatorGroupAssign.id
}
}
Write-Verbose -Verbose "Added operator group '$($operatorGroupAssign.groupName)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get operator groups, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get operator groups, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if(!$incidentObject.ProcessingStatus -and $incidentObject.CloseTicket -eq "True"){
Write-Verbose -Verbose "The ticket needs to be closed, but no processing status has been given. Searching for closed processing status"
Add-Member -InputObject $incidentObject -NotePropertyName ProcessingStatus -NotePropertyValue Closed
}
if($incidentObject.ProcessingStatus){
Write-Verbose -Verbose "Getting processing states"
try{
$responseProcessingStatus = Invoke-WebRequest -Uri $uriProcessingStatus -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$processingStatusAssign = $responseProcessingStatus | Where-Object{$_.name -eq $incidentObject.ProcessingStatus -or $_.processingState -eq $incidentObject.ProcessingStatus}
if($null -eq $processingStatusAssign){
Write-Verbose -Verbose "Processing status '$($incidentObject.ProcessingStatus)' not found, processing status is ignored"
}elseif("2" -le $processingStatusAssign.count){
Write-Verbose -Verbose "Multiple processing statusses '$($incidentObject.ProcessingStatus)' found, processing status is ignored"
}else{
$requestObject += @{
processingStatus = @{
id = $processingStatusAssign.id
}
}
Write-Verbose -Verbose "Added processing status '$($processingStatusAssign.name)' to request"
if($processingStatusAssign.processingState -ne "Closed" -and $incidentObject.CloseTicket -eq "True"){
Write-Verbose -Verbose "Processing status '$($processingStatusAssign.name)' does not have a closed state, so close incident is ignored"
}
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get processing status, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get processing status, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.Category){
Write-Verbose -Verbose "Getting categories"
try{
$responseCategories = Invoke-WebRequest -Uri $uriCategories -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$categoryAssign = $responseCategories | Where-Object{$_.name -eq $incidentObject.Category}
if($null -eq $categoryAssign){
Write-Verbose -Verbose "Category '$($incidentObject.Category)' not found, category is ignored"
}elseif("2" -le $categoryAssign.count){
Write-Verbose -Verbose "Multiple categories '$($incidentObject.Category)' found, category is ignored"r
}else{
$requestObject += @{
category = @{
id = $categoryAssign.id
}
}
Write-Verbose -Verbose "Added category '$($categoryAssign.name)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get category errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get category, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.SubCategory -and $incidentObject.Category){
Write-Verbose -Verbose "Getting subcategories"
try{
$responseSubCategories = Invoke-WebRequest -Uri $uriSubCategories -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$subCategoryAssign = $responseSubCategories | Where-Object{($_.name -eq $incidentObject.SubCategory) -and ($_.category.name -eq $incidentObject.Category)}
if($null -eq $subCategoryAssign){
Write-Verbose -Verbose "Subcategory '$($incidentObject.SubCategory)' not found, subcategory is ignored"
}elseif("2" -le $subCategoryAssign.count){
Write-Verbose -Verbose "Multiple subcategories '$($incidentObject.SubCategory)' found, subcategory is ignored"
}else{
Write-Verbose -Verbose "Subcategory '$($subCategoryAssign.name)' belongs to category '$($subCategoryAssign.category.name)'. Assigning categories"
$requestObject += @{
subcategory = @{
id = $subCategoryAssign.id
}
}
Write-Verbose -Verbose "Added subcategory '$($subCategoryAssign.name)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get categories, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get categories, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.Impact){
Write-Verbose -Verbose "Getting impacts"
try{
$responseImpacts = Invoke-WebRequest -Uri $uriImpacts -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$impactAssign = $responseImpacts | Where-Object{$_.name -eq $incidentObject.Impact}
if($null -eq $impactAssign){
Write-Verbose -Verbose "Impect '$($incidentObject.Impact)' not found, impact is ignored"
}elseif("2" -le $impactAssign.count){
Write-Verbose -Verbose "Multiple impacts '$($incidentObject.Impact)' found, impact is ignored"
}else{
$requestObject += @{
impact = @{
id = $impactAssign.id
}
}
Write-Verbose -Verbose "Added impact '$($impactAssign.name)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get impact, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get impact, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.Urgency){
Write-Verbose -Verbose "Getting urgencies"
try{
$responseUrgencies = Invoke-WebRequest -Uri $uriUrgencies -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$urgencyAssign = $responseUrgencies | Where-Object{$_.name -eq $incidentObject.Urgency}
if($null -eq $urgencyAssign){
Write-Verbose -Verbose "Urgency '$($incidentObject.Urgency)' not found, urgency is ignored"
}elseif("2" -le $urgencyAssign.count){
Write-Verbose -Verbose "Multiple urgencies '$($incidentObject.Urgency)' found, urgency is ignored"
}else{
$requestObject += @{
urgency = @{
id = $urgencyAssign.id
}
}
Write-Verbose -Verbose "Added urgency '$($urgencyAssign.name)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get urgency, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get urgency, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.Priority){
Write-Verbose -Verbose "Getting priorities"
try{
$responsePriorities = Invoke-WebRequest -Uri $uriPriorities -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$priorityAssign = $responsePriorities | Where-Object{$_.name -eq $incidentObject.Priority}
if($null -eq $priorityAssign){
Write-Verbose -Verbose "Priority '$($incidentObject.Priority)' not found, priority is ignored"
}elseif("2" -le $priorityAssign.count){
Write-Verbose -Verbose "Multiple priorities '$($incidentObject.Priority)' found, priority is ignored"
}else{
$requestObject += @{
priority = @{
id = $priorityAssign.id
}
}
Write-Verbose -Verbose "Added priority '$($priorityAssign.name)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get priority, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get priority, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.CallType){
Write-Verbose -Verbose "Getting calltypes"
try{
$responseCallTypes = Invoke-WebRequest -Uri $uriCallTypes -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$callTypeAssign = $responseCallTypes | Where-Object{$_.name -eq $incidentObject.CallType}
if($null -eq $callTypeAssign){
Write-Verbose -Verbose "Call type '$($incidentObject.CallType)' not found, call type is ignored"
}elseif("2" -le $callTypeAssign.count){
Write-Verbose -Verbose "Multiple call types '$($incidentObject.CallType)' found, call type is ignored"
}else{
$requestObject += @{
callType = @{
id = $callTypeAssign.id
}
}
Write-Verbose -Verbose "Added call type '$($callTypeAssign.name)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get call type, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get call type, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.Entry){
Write-Verbose -Verbose "Getting Entry types" -Event Information
try{
$responseEntryTypes = Invoke-WebRequest -Uri $uriEntryTypes -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$entryTypeAssign = $responseEntryTypes | Where-Object{$_.name -eq $incidentObject.Entry}
if($null -eq $entryTypeAssign){
Write-Verbose -Verbose "Entry type '$($incidentObject.Entry)' not found, entry type is ignored"
}elseif("2" -le $entryTypeAssign.count){
Write-Verbose -Verbose "Multiple entry types '$($incidentObject.Entry)' found, entry type is ignored"
}else{
$requestObject += @{
entryType = @{
id = $entryTypeAssign.id
}
}
Write-Verbose -Verbose "Added entry type '$($entryTypeAssign.name)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get entry type, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get entry type, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
if($incidentObject.CallerEmail){
if ($incidentObject.CallerEmail -ne "manager" -and $incidentObject.CallerEmail -ne "employee") {
try{
$responseCallerEmail = Invoke-WebRequest -Uri $uriCallerEmail -Method GET -ContentType $contentType -Headers $headers -UseBasicParsing | ConvertFrom-Json
$callerEmailAssign = $responseCallerEmail | Where-Object{$_.email -eq $incidentObject.CallerEmail}
if($null -eq $callerEmailAssign){
throw "Caller email '$CallerEmail' not found, a valid caller email must be specified"
}elseif("2" -le $callTypeAssign.count){
throw "Multiple caller emails '$CallerEmail' found, specify a unique caller email"
}else{
$requestObject += @{
callerLookup = @{
email = $callerEmailAssign.email
}
}
Write-Verbose -Verbose "Added caller email '$($callerEmailAssign.email)' to request"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not get caller email, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not get caller email, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
}
else {
if ($incidentObject.CallerEmail -eq "employee") {
$requestObject += @{
callerLookup = @{
id = $aRef
}
}
} else {
$requestObject += @{
callerLookup = @{
id = $mRef
}
}
}
Write-Verbose -Verbose "Added requester $($CallerEmail.CallerEmail) to request"
}
}
if($incidentObject.Status){
$requestObject += @{
status = "secondLine"
}
}else{
$requestObject += @{
status = "firstLine"
}
}
Write-Verbose -Verbose "Added status '$($requestObject.status)' to request"
if($incidentObject.TargetDate){
try{
$datetime = [System.DateTime]::ParseExact($incidentObject.TargetDate,"dd-MM-yyyy",$null)
$requestObject += @{
targetDate = $datetime.ToString("yyyy-MM-ddTHH:mm:ms.fffzz00")
}
Write-Verbose -Verbose "Added target date '$($incidentObject.TargetDate)' to request"
}catch{
Write-Verbose -Verbose "The target date '$($incidentObject.TargetDate)' has an invalid format. The incident will be created without a target date."
}
}
if($incidentObject.ObjectId){
$requestObject += @{
object = @{
name = $ObjectId
}
}
Write-Verbose -Verbose "Added object ID '$($incidentObject.ObjectId)' to request"
}
if($incidentObject.RequestDescription){
$requestObject += @{
request = $incidentObject.RequestDescription
}
Write-Verbose -Verbose "Added request description '$($incidentObject.RequestDescription)' to request"
}
if($incidentObject.RequestShort){
$requestObject += @{
briefDescription = $($incidentObject.RequestShort)
}
write-verbose -verbose $incidentObject.RequestShort
Write-Verbose -Verbose "Added brief description '$($incidentObject.RequestShort)' request"
}
$request = $requestObject | ConvertTo-Json -Depth 10
Write-Verbose -Verbose "Start creating ticket"
try{
Write-Verbose -Verbose "Starting to create TOPdesk incident '$($incidentObject.RequestShort)'"
$response = Invoke-WebRequest -Uri $uriIncidents -Method POST -ContentType $contentType -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($request)) -UseBasicParsing
$incident = $response.Content | ConvertFrom-Json
if($CloseTicket -eq "True"){
Write-Verbose -Verbose "Successfully created TOPdesk incident '$($incidentObject.RequestShort)', immediately closed the ticket"
}else{
Write-Verbose -Verbose "Successfully created TOPdesk incident '$($incidentObject.RequestShort)'"
}
}catch{
if($_.Exception.Message -eq "The remote server returned an error: (400) Bad Request."){
$message = ($_.ErrorDetails.Message | convertFrom-Json).message
throw "Could not create incident, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message) $message"
}else{
throw "Could not create incident, errorcode: '0x$('{0:X8}' -f $_.Exception.HResult)', message: $($_.Exception.Message)"
}
}
Write-Verbose -Verbose "Succesfully created TOPdesk incident '$($incidentObject.RequestShort)' with number '$($incident.number)', check the Progress Log for details"
return $incident
}
$incidentList = Get-Content -Raw -Path $path | ConvertFrom-Json
$incident = $incidentList | Where-Object { ($_.Identification.Id -eq $pRef.id) -and ($_.HelloIDAction -eq "Grant") } | Select-Object -Property * -ExcludeProperty DisplayName, Identification, HelloIDAction
if (-Not($dryRun -eq $True)) {
if (![string]::IsNullOrEmpty($incident)) {
try {
$incident.RequestDescription = Invoke-Expression "`"$($incident.RequestDescription)`""
$incident.RequestShort = Invoke-Expression "`"$($incident.RequestShort)`""
$incidentResult = New-TopdeskIncident -incidentObject $incident
$success = $True;
$auditMessage = "Succesfully created TOPdesk incident with number $($incidentResult.number)"
}
catch {
write-verbose -verbose $_
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$errResponse = $reader.ReadToEnd();
$auditMessage = "${errResponse}";
}
}
}
#build up result
$result = [PSCustomObject]@{
Success= $success;
AccountReference= $aRef;
AuditDetails=$auditMessage;
Account= $account;
};
Write-Output $result | ConvertTo-Json -Depth 10;