Hi Everyone,
I have noticed several questions around REST APIs for Service Desk Plus on the forums, and some vague answers. I have been using the REST APIs as part of a daily PowerShell workflow for some time, and I thought I would post my PowerShell functions with the hope of helping others through an example.
Please keep in mind that these are very basic functions, and they are not intended to be a one-size-fits-all module. So please modify to meet your needs, and feel free to post your changes so others can get help on this very useful feature.
I am using four basic functions
- Get a request, or several requests, based on a view filter
- Add a work item to a request
- Add a resolution to a request
- Close a request
Below the four functions is an example on how to use them.
- function Get-Request
{
<#
.SYNOPSIS
This function is used to return a list of requests. If RequestID is specified then a single request is returned. If RequestID is ommited then all requests assigned to the specified filter are returned. The filter will default to All Requests if one is not specified.
.DESCRIPTION
Get a list of Requests
- .EXAMPLE
Get-Request -SdpUri "http://sdp.domain.com" -ApiKey "1234567A-2AB0-12A3-A123-1234567890AB" -RequestID 1234
- .PARAMETER RequestID
The RequestID is the integer assigned to a "ticket" automatically by service desk plus
- .PARAMETER Filter
The Request View Filter. These filters can be created in SDP. To find the filter name, Click on the ADMIN tab in SDP. Under General Settings click API, and Documentation.
In the new Tab click "Requests" then "View Requests Filters", then "Try now". This parameter accepts the VIEWID
.PARAMETER ApiKey
This parameter is the API Key assigned to each technician. This serves as authentication to Service Desk Plus
.PARAMETER Limit
This parameter limits the number of returned requests. the default is 50
- .PARAMETER SdpUri
This is the URI for Service Desk Plus
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=0)]
[alias ("id")]
[Int32]
$RequestID=$null,
- [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=1)]
[String]
$Filter = "All_Requests",
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)]
[String]
$ApiKey,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)]
[String]
$SdpUri,
- [Parameter(Mandatory=$false)]
[String]
$limit = 50
)
begin
{
if ($SdpUri[$SdpUri.Length -1] -eq "/") { $Uri = $SdpUri + "sdpapi/request" }
else { $Uri = $SdpUri + "/sdpapi/request" }
}
process
{
if ($RequestID -gt 0)
{
$Uri = $Uri + "/" + $RequestID
$Uri = $Uri + "?format=json&OPERATION_NAME=GET_REQUEST&TECHNICIAN_KEY=$ApiKey"
$result = Invoke-RestMethod -Method Get -Uri $Uri
$result
}
else
{
$Parameters = @{
"operation" = @{
"details" = @{
"from" = "0";
"limit" = $limit;
"filterby" = $filter
}
}
}
-
- $input_data = $Parameters | ConvertTo-Json -Depth 50
$Uri = $Uri + "?format=json&OPERATION_NAME=GET_REQUESTS&INPUT_DATA=$input_data&TECHNICIAN_KEY=$ApiKey"
$result = Invoke-RestMethod -Method Get -Uri $Uri
$result
}
}
}
function Add-WorkItem
{
<#
.SYNOPSIS
This function adds a work item to a request with a known request ID
.DESCRIPTION
Add a work item to a request
.EXAMPLE
Add-WorkItem -RequestID $request.WORKORDERID -ApiKey $ApiKey -SdpUri $SdpUri -Description "Review Completed" -workHours 0 -WorkMinutes 15 -Technician "Darth Vader"
.PARAMETER RequestID
The RequestID is the integer assigned to a "ticket" automatically by service desk plus
- .PARAMETER SdpUri
This is the URI for Service Desk Plus
.PARAMETER ApiKey
This parameter is the API Key assigned to each technician. This serves as authentication to Service Desk Plus
.PARAMETER Description
This is the text that will display in the work item description area.
.PARAMETER Technician
This is the technician assigned to the work item
.PARAMETER WorkHours
This is the hours worked on a work item
.PARAMETER WorkMinutes
This is the minutes worked on a work item
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[alias ("id")]
[Int32]
$RequestID,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)]
[String]
$ApiKey,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)]
[String]
$SdpUri,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]
$Description,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]
$Technician,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Int32]
$WorkHours,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Int32]
$WorkMinutes
)
begin
{
if ($SdpUri[$SdpUri.Length -1] -eq "/") { $Uri = $SdpUri + "sdpapi/request/$RequestID/worklogs" }
else { $Uri = $SdpUri + "/sdpapi/request/$RequestID/worklogs" }
}
process
{
$Parameters = @{
"operation" = @{
"details" = @{
"worklogs" = @{
"worklog" = @{
"description" = $Description;
"technician" = $Technician;
"workHours" = $WorkHours;
"workMinutes" = $WorkMinutes
}
}
}
}
}
-
- $input_data = $Parameters | ConvertTo-Json -Depth 50
$Uri = $Uri + "?format=json&OPERATION_NAME=ADD_WORKLOG&INPUT_DATA=$input_data&TECHNICIAN_KEY=$ApiKey"
$result = Invoke-RestMethod -Method POST -Uri $Uri
$result
}
}
function Set-Resolution
{
<#
.SYNOPSIS
Set the Resolution for a request
.DESCRIPTION
Set the Resolution for a request
.EXAMPLE
Set-Resolution -RequestID $request.WORKORDERID -ApiKey $ApiKey -SdpUri $SdpUri -Resolution "I rebooted three times"
.PARAMETER RequestID
The RequestID is the integer assigned to a "ticket" automatically by service desk plus
- .PARAMETER SdpUri
This is the URI for Service Desk Plus
.PARAMETER ApiKey
This parameter is the API Key assigned to each technician. This serves as authentication to Service Desk Plus
.PARAMETER Resolution
This is the text that will be added as a resolution
-
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[alias ("id")]
[Int32]
$RequestID,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)]
[String]
$ApiKey,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)]
[String]
$SdpUri,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]
$Resolution
)
begin
{
if ($SdpUri[$SdpUri.Length -1] -eq "/") { $Uri = $SdpUri + "sdpapi/request/$RequestID/resolution" }
else { $Uri = $SdpUri + "/sdpapi/request/$RequestID/resolution" }
}
process
{
$Parameters = @{
"operation" = @{
"details" = @{
"resolution" = @{
"resolutiontext" = $resolution
}
}
}
}
-
- $input_data = $Parameters | ConvertTo-Json -Depth 50
$Uri = $Uri + "?format=json&OPERATION_NAME=ADD_RESOLUTION&INPUT_DATA=$input_data&TECHNICIAN_KEY=$ApiKey"
$result = Invoke-RestMethod -Method POST -Uri $Uri
$result
}
}
function Close-Request
{
<#
.SYNOPSIS
Close a request. This function assumes the closure has been acknowledged.
.DESCRIPTION
Close a request. This function assumes the closure has been acknowledged.
.EXAMPLE
Close-Request -RequestID $request.WORKORDERID -ApiKey $ApiKey -SdpUri $SdpUri
.PARAMETER RequestID
The RequestID is the integer assigned to a "ticket" automatically by service desk plus
- .PARAMETER SdpUri
This is the URI for Service Desk Plus
.PARAMETER ApiKey
This parameter is the API Key assigned to each technician. This serves as authentication to Service Desk Plus
-
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[alias ("id")]
[Int32]
$RequestID,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)]
[String]
$ApiKey,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)]
[String]
$SdpUri
)
begin
{
if ($SdpUri[$SdpUri.Length -1] -eq "/") { $Uri = $SdpUri + "sdpapi/request/$RequestID" }
else { $Uri = $SdpUri + "/sdpapi/request/$RequestID" }
}
process
{
$Parameters = @{
"operation" = @{
"details" = @{
"closeAccepted" = "Accepted";
"closeComment" = "Review Completed"
}
}
}
-
- $input_data = $Parameters | ConvertTo-Json -Depth 50
$Uri = $Uri + "?format=json&OPERATION_NAME=CLOSE_REQUEST&INPUT_DATA=$input_data&TECHNICIAN_KEY=$ApiKey"
$result = Invoke-RestMethod -Method POST -Uri $Uri
$result
}
}
Now here an the example on how I use those four functions:
- $ApiKey = "YOUR API KEY HERE!"
$SdpUri = "https://sdpURL.domain.com"
$MyDailyPMRequests = Get-Request -ApiKey $ApiKey -SdpUri $SdpUri -Filter "Open_User" -Limit 0
foreach ($request in $$MyDailyPMRequests.operation.details)
{
if ($request.SUBJECT -like "*TICKET I WANT TO CLOSE*")
{
$result = Set-Resolution -RequestID $request.WORKORDERID -ApiKey $ApiKey -SdpUri $SdpUri -Resolution "I DID MY DAILY WORK"
$result.operation.result.message
- $result = Add-WorkItem -RequestID $request.WORKORDERID -ApiKey $ApiKey -SdpUri $SdpUri -Description "I PERFORMED X Y AND Z" -workHours 0 -WorkMinutes 15 -Technician "René Sommer"
$result.operation.result.message
- $result = Close-Request -RequestID $request.WORKORDERID -ApiKey $ApiKey -SdpUri $SdpUri
$result.operation.result.message
}
}
I hope this helps someone!
Good luck!
Robert