-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceDeskPlus.psm1
260 lines (237 loc) · 6.95 KB
/
ServiceDeskPlus.psm1
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
$ApiKey = get-content .\config\sdp_apikey.txt
$SdpUri = get-content .\config\sdp_server.txt
# Pulls info about an existing request
function Get-Ticket
{
<#
.DESCRIPTION
Pulls ticket info from Service Desk Plus.
.PARAMETER RequestID
Request ID of the ticket to modify
.EXAMPLE
Get-Ticket -RequestID 12345
.NOTES
Author: Andre Wroblewski
Date: 28-November-2019
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[alias ("id")]
[Int32] $RequestID
)
$input= @"
{
"request":{
"udf_fields":{
"udf_sline_18083": "$RequestFor"
}
}
}
"@
$header = @{TECHNICIAN_KEY=$ApiKey}
$params = @{input_data=$input;format='json'}
$Uri = $SdpUri + "/api/v3/requests/" + $RequestID
$result = Invoke-RestMethod -Method Get -Uri $Uri -Headers $header
$description = (Convert-HtmlToText $result.request.description).Replace(" ", " ")
clear
write-host "Subject:" $result.request.subject
"----------------------------------------------------------------"
write-host "Technician:" $result.request.technician.name
write-host "Has conversation:" $result.request.has_conversation
write-host "Responded time:" $result.request.responded_time.display_value
write-host "status:" $result.request.status.name
write-host "Has notes:" $result.request.has_notes
Out-Notepad "Ticket Description: `n $description"
#Display Notes if any
if ($result.request.has_notes -eq "True") {
#Lookup Note IDs
$UriNotesID = $SdpUri + "/api/v3/requests/" + $RequestID + "/notes"
$resultNotesID = Invoke-RestMethod -Method Get -Uri $UriNotesID -Headers $header
#Get Notes
foreach ($id_note in $resultNotesID.notes.id) {
$UriNotes = $SdpUri + "/api/v3/requests/" + $RequestID + "/notes/" + $id_note
$resultNotes = Invoke-RestMethod -Method Get -Uri $UriNotes -Headers $header
#Display Note
$date = $resultNotes.request_note.created_time.display_value
$note = Convert-HtmlToText $resultNotes.request_note.description
write-host "Note $id_note ........ Opening Notepad Window"
$note1 = "Note ID: $id_note `n Date Created $date `n `n $note `n `n"
$output += $note1
}
out-notepad "Ticket Notes: `n `n $output"
}
#Display Resolution if any
if (($result.request.resolution.content).count -ne 0) {
$UriResolution = $SdpUri + "/api/v3/requests/" + $RequestID + "/resolutions"
$resultResolution = Invoke-RestMethod -Method Get -Uri $UriResolution -Headers $header
#Display Resolution
$Resolution = Convert-HtmlToText $resultResolution.resolution.content
write-host "Resolution ........ Opening Notepad Window."
Out-Notepad "Resolution: `n $Resolution"
}
}
#Adds resolution and closes ticket
function Resolve-Ticket
{
<#
.DESCRIPTION
Used to add resolution and close ticket.
.PARAMETER RequestID
Request ID of the ticket to modify
.PARAMETER Resolution
Ticket resolution
.PARAMETER Status
Change status of the ticket to Resolved or Closed. Alternatively leave it open.
.EXAMPLE
Resolve-Ticket -RequestID 12345 -Resolution "Issue resolved" -Status "Closed"
.NOTES
Forked from https://github.com/GarySmithPS/SDP-Module
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[alias ("id")]
[Int32] $RequestID,
[Parameter(Mandatory = $true, Position=1)]
[string]$Resolution,
[Parameter(Mandatory = $true, Position=2)]
[ValidateSet("Open", "Resolved", "Closed")]
[string]$Status
)
process {
$inputData = @"
{
"request": {
"resolution": {
"content": "$Resolution"
},
"status": {
"name": "$Status"
}
}
}
"@
$URI = $SdpUri + "/api/v3/requests/$RequestID" + "?TECHNICIAN_KEY=$ApiKey&input_data=$inputdata&format=json"
Invoke-WebRequest -Method PUT -Uri $URI -UseBasicParsing -Verbose
}
}
#Adds note to a ticket
Function Add-NoteToTicket
{
<#
.DESCRIPTION
Adds note to existing ticket.
.PARAMETER RequestID
Request ID of the ticket to modify
.PARAMETER Note
Text of the note that you want to add
.EXAMPLE
Add-NoteToTicket -RequestID 12345 -Note "This is a note"
.NOTES
Author: Andre Wroblewski
Date: 28-November-2019
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[alias ("id")]
[Int32] $RequestID,
[Parameter(Mandatory = $true, Position=1)]
[string] $Note
)
$inputData = @"
{
"request_note": {
"description": "$Note",
"show_to_requester": false,
"notify_technician": false,
"mark_first_response": false,
"add_to_linked_requests": false
}
}
"@
$header = @{TECHNICIAN_KEY=$ApiKey}
$params = @{input_data=$inputData;format='json'}
$Uri = $SdpUri + "/api/v3/requests/$RequestID" + "/notes"
Invoke-RestMethod -Method POST -Uri $Uri -Headers $header -Body $params -ContentType "application/x-www-form-urlencoded" -verbose
}
##################################
#
#
# Support functions
#
#
#################################
function Convert-HtmlToText
{
<#
.DESCRIPTION
Converts html output to plain text.
.PARAMETER html
HTML output
.EXAMPLE
Convert-HtmlToText $html_output
.NOTES
Author http://winstonfassett.com/blog/2010/09/21/html-to-text-conversion-in-powershell/
#>
param([System.String] $html)
# remove line breaks, replace with spaces
$html = $html -replace "(`r|`n|`t)", " "
# write-verbose "removed line breaks: `n`n$html`n"
# remove invisible content
@('head', 'style', 'script', 'object', 'embed', 'applet', 'noframes', 'noscript', 'noembed') | % {
$html = $html -replace "<$_[^>]*?>.*?</$_>", ""
}
# write-verbose "removed invisible blocks: `n`n$html`n"
# Condense extra whitespace
$html = $html -replace "( )+", " "
# write-verbose "condensed whitespace: `n`n$html`n"
# Add line breaks
@('div','p','blockquote','h[1-9]') | % { $html = $html -replace "</?$_[^>]*?>.*?</$_>", ("`n" + '$0' )}
# Add line breaks for self-closing tags
@('div','p','blockquote','h[1-9]','br') | % { $html = $html -replace "<$_[^>]*?/>", ('$0' + "`n")}
# write-verbose "added line breaks: `n`n$html`n"
#strip tags
$html = $html -replace "<[^>]*?>", ""
# write-verbose "removed tags: `n`n$html`n"
# replace common entities
@(
@("&bull;", " * "),
@("&lsaquo;", "<"),
@("&rsaquo;", ">"),
@("&(rsquo|lsquo);", "'"),
@("&(quot|ldquo|rdquo);", '"'),
@("&trade;", "(tm)"),
@("&frasl;", "/"),
@("&(quot|#34|#034|#x22);", '"'),
@('&(amp|#38|#038|#x26);', "&"),
@("&(lt|#60|#060|#x3c);", "<"),
@("&(gt|#62|#062|#x3e);", ">"),
@('&(copy|#169);', "(c)"),
@("&(reg|#174);", "(r)"),
@("&nbsp;", " "),
@("&(.{2,6});", "")
) | % { $html = $html -replace $_[0], $_[1] }
# write-verbose "replaced entities: `n`n$html`n"
return $html
}
Function Out-Notepad
{
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
$StrText
)
$fso=new-object -com scripting.filesystemobject
$filename=$fso.GetTempName()
$tempfile=Join-Path $env:temp $filename
$strText | Out-File $tempfile
notepad $tempfile
#tidy up
sleep 2
if (Test-Path $tempfile) {del $tempfile}
}