-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.ps1
More file actions
73 lines (69 loc) · 3.18 KB
/
action.ps1
File metadata and controls
73 lines (69 loc) · 3.18 KB
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
# TOPdesk-Task-SA-Target-TOPdesk-ChangeCreate
###########################################################
# Form mapping
$formObject = @{
requester = $form.requester
briefDescription = $form.briefDescription
request = $form.request
action = $form.action
changeType = $form.changeType
template = $form.template
category = $form.category
subcategory = $form.subcategory
externalNumber = $form.externalNumber
impact = $form.impact
benefit = $form.benefit
priority = $form.priority
}
try {
Write-Information "Executing TOPdesk action: [CreateChange] for: [$($formObject.briefDescription)]"
Write-Verbose "Creating authorization headers"
# Create authorization headers with TOPdesk API key
$pair = "${topdeskApiUsername}:${topdeskApiSecret}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$key = "Basic $base64"
$headers = @{
"authorization" = $Key
"Accept" = "application/json"
"Partner-Solution-Id" = "TOOL001"
}
Write-Verbose "Creating TOPdesk Change: [$($formObject.briefDescription)]"
$splatCreateChangeParams = @{
Uri = "$($topdeskBaseUrl)/tas/api/operatorChanges"
Method = "POST"
Body = ([System.Text.Encoding]::UTF8.GetBytes(($formObject | ConvertTo-Json -Depth 10)))
Verbose = $false
Headers = $headers
ContentType = "application/json; charset=utf-8"
}
$response = Invoke-RestMethod @splatCreateChangeParams
$auditLog = @{
Action = "CreateResource"
System = "TOPdesk"
TargetIdentifier = [String]$response.id
TargetDisplayName = [String]$response.number
Message = "TOPdesk action: [CreateChange] for: [$($formObject.briefDescription)] executed successfully"
IsError = $false
}
Write-Information -Tags "Audit" -MessageData $auditLog
Write-Information "TOPdesk action: [CreateChange] for: [$($formObject.briefDescription)] executed successfully"
}
catch {
$ex = $_
$auditLog = @{
Action = "CreateResource"
System = "TOPdesk"
TargetIdentifier = ""
TargetDisplayName = [String]$formObject.briefDescription
Message = "Could not execute TOPdesk action: [CreateChange] for: [$($formObject.briefDescription)], error: $($ex.Exception.Message)"
IsError = $true
}
if ($($ex.Exception.GetType().FullName -eq "Microsoft.PowerShell.Commands.HttpResponseException")) {
$auditLog.Message = "Could not execute TOPdesk action: [CreateChange] for: [$($formObject.briefDescription)]"
Write-Error "Could not execute TOPdesk action: [CreateChange] for: [$($formObject.briefDescription)], error: $($ex.ErrorDetails)"
}
Write-Information -Tags "Audit" -MessageData $auditLog
Write-Error "Could not execute TOPdesk action: [CreateChange] for: [$($formObject.briefDescription)], error: $($ex.Exception.Message)"
}
###########################################################