Using the REST API in Manage Engine ServiceDesk Plus with PowerShell

It has been a while since my last post.  I have changed jobs and started focusing more on scripting and automation so I thought I would finally write a new post.  PowerShell is a very powerful scripting language and I have taken advantage of added tools in PowerShell 3.0 with regards to http web requests and Invoke-WebRequest.

ServiceDesk Plus by Manage Engine is a ticketing system with the ability to interact with it via their API.  By combining PowerShell Invoke-WebRequest and ServiceDesk’s REST API you can do all kinds of cool things like open and close tickets or even pull info out of a ticket and perform some automated tasks.  Interfacing with the API is pretty straight forward and easy to get started with but in order to do that you will need a few things to get started.

1.) You will need to get some information about your ServiceDesk Installation:
-You will need to get the IP or DNS name of your servicedesk server.
-You will also need to get the Port Number set for your ServiceDesk server.
-This could be 80 or 8080 or another port that you set.  If you are not sure when you start up ServiceDesk the command line interface should tell you the port number.

2.) You will need to get an API key from an active ServiceDesk Technician account:
goto Admin -> Technicicians -> find the technician -> edit the technician -> at the bottom click Generate API Key (Be sure to save after generating)

NOTE: That you cannot generate an API key for your own account so you will need to get another admin account to generate it for you or in my case I created a API service Account and made it a technician to use for my scripts

Once you have this information you are ready to start building a request! For this example we will build an Add Request  to generate a new ticket.

To start based on the ServiceDesk API documentation for the REST API we want to start by building the http request:

$inputData = @"
<requester>John Doe</requester>
<subject>Testing 123</subject>
<description>hello world!</description>
<requesttemplate>Default Request</requesttemplate>

Esseintially we are creating a block text with all our request details. These can easily be replaced with variables so you can dynamically generate a request.  Next we need to create the post parameters for the request:

$postParams = @{OPERATION_NAME='ADD_REQUEST';TECHNICIAN_KEY='<technician Key>';INPUT_DATA=$inputData}

This builds a post command for the request using the technician key from earlier to submit the request to ServiceDesk.  This allows you to submit requests with out ever passing any login credentials.

Finally we are ready to put it all together and submit the request:

Invoke-WebRequest -Uri $URI -Method POST -Body $postParams

This is the simplest way to submit a request and will return the http response from service desk which is a long list of properties.  Most of which is not important but what you will probably care about is the content property which is an XML response with details about your new ticket and looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<API version="1.0">
<operation name="ADD_REQUEST">
<message>Request added successfully</message>

This returns the details of a successful or unsuccessful request.  In this case the request status message was “Request added successfully” and it also returned the ticket ID number of 7300.  While this is great you probably want to do something usefull with this information such as make sure the request was successfull and maybe strore the ticket number some where.

In this case we can convert the content response to XML and reference specific property fields:

$response = Invoke-WebRequest -Uri $URI -Method POST -Body $postParams
[xml]$responseContent = $response.Content
if($responseContent.API.response.operation.result.status -eq "Success")
$reqID = [string]$responseContent.API.response.operation.Details.workorderid

Here we check to make sure the result of the request was a success and if so we grab the ticket ID number.

This is just an example of what you can do or the fields in a servicedesk ticket that you can update.  Pretty much any field in a request template can be updated as long as you reference the field name.  There is also alot more that you can do with the REST API such as update a ticket, get details of and existing ticket or even use ticket details to run an automated task such as create a new distribution list based on a request.  I will try and add some more posts on what you can do in the future.

About Dane

Currently working on scripting and IT automation. My skill set includes IDM, Active Directory and Exchange Administration, powershell scripting and more...
This entry was posted in Powershell. Bookmark the permalink.

6 Responses to Using the REST API in Manage Engine ServiceDesk Plus with PowerShell

  1. PumexDev says:

    Can you help me to fetch tickets for a particular account.

    • Dane says:

      Are you trying to fetch all tickets from a specific requestor or technician? I dont think that can be done from the api. You are better off searching directly on the database either with a report or if you need to get it programmatically query the database with your code. The easiest way to do that is create a custom report in service desk that gets you the info you want then click the edit the query button to get the sql query. Then copy it and build that into your code.

  2. Thiago Paz Costa da Silva says:

    Can you send me that full powershell file? You show it in parts. Is it possible convert this powershell to linux sh. extension?

    Thanks Dane

  3. DAle says:

    Do you know of a way to query any request field value?

  4. Thiago Paz says:

    Thanks for post this.

    I need send a ticket id to a e-mail account. How can i send the ticket id from response by e-mail in the same script?

    Thanks again

Leave a Reply to Dane Cancel reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>