**** This entire article now available as a white paper for download here >>  ****


There are currently two main options used to run PowerShell scripts from ServiceDesk Plus, the ‘Request Custom Menu’ or ‘Custom Trigger’ options, available from ‘Admin > HelpDesk Customiser’. Both of these configurations will have an option to execute a script and, in order to run a PowerShell script, you will need to use the following command as an ‘Execute Script’ action:

 
cmd /c powershell.exe -WindowStyle Hidden -file c:ManageEngineServiceDeskintegrationcustom_scriptstestscript.ps1 $COMPLETE_JSON_FILE

The use of the tag $COMPLETE_JSON_FILE is only needed when you want to send your PowerShell script the output data of the request from where the script is being actioned or triggered. This is useful for getting information such as the Request ID if you’re looking to interact with the request from which the PowerShell script was actioned. You can pass individual request data values, e.g. $ASSET, but you might as well send details of the whole request and split out the required values as needed in the PowerShell script.

So for a real scenario … one client we were working with had a new starter process that required an email to be delivered to a specific team that were not part of the ServiceDesk installation with details of the ticket if a particular level of access was requested. 

In order to achieve this we designed a Service Request template with all the necessary data fields and resource elements to collect the required data for the new starter process. On this template was a specific custom field that we used to specify the particular access level and this was used as a trigger condition for a ‘Custom Trigger’ with the full request data being passed to the script as $COMPLETE_JSON_FILE. 

An example is shown below:




APIPart4_1



Here is the PowerShell script we created to send the email with the required contents of the request; the actual script also included a large number of custom fields but I’ve only included the standard SDP request fields in this example. I’ve also kept the data in the JSON format as this easier to relate to and test when using the API Documentation feature in the ServiceDesk Plus Admin tab. Remember to save your script file to the ‘integrationcustom_scripts’ folder if you’re going to test a copy:

——– Start of script ——-
# V1.0 Initial release
# V1.1 Updated to use API to send Email as Reply to Request rather than PowerShell 
# MailMessage Cmdlet

# Store request data passed to script – this must be first element in script 
param (
$json = “none”
)

$jsondata = Get-Content $json -Encoding UTF8   #Encode if necessary
$obj = ConvertFrom-Json $jsondata

# Add Request fields values as needed – custom fields are referenced by their alias name
$workorderid = $obj.request.WORKORDERID
$requester = $obj.request.REQUESTER
$createdtime = $obj.request.createdtime
$subject = $obj.request.SUBJECT
$category = $obj.request.category
$technician = $obj.request.technician
$status = $obj.request.status
$priority = $obj.request.priority
$requesttype = $obj.request.requesttype
$group = $obj.request.group
$description = $obj.request.description

# Set system parameters – change these details to suit your system environment
$sdphost = “http://[hostname or IP address]:[port]/”
$techkey = “[your technician API key]

# Set API module URL and operation – this is the URL for calling the request API
# It uses the request parameter passed to the script $workorderid
$url = $sdphost + “sdpapi/request/” + $workorderid
$method = “POST”
$operation = “REPLY_REQUEST”

# Define Email Content
$to = ” [insert to email address here] ”
$cc = ” [insert cc email address here] 
$subject = “New Starter Alert”

# email description text 
$body = “Request Number : $workorderid, Requester : $requester, Subject : $subject, Category : $category, Technician: $technician, Status : $status, Priority : $priority, Request Type: $requesttype, Group : $group”

# Configure input data for email Reply operation in JSON format
$inputdata = @”
{
“operation”: {
“details”: {
“to”: “$to”,
“cc”: “$cc”,
“subject”: “$subject”,
“description”: “$body”
}
}
}
“@

# Configure paramaters for web API call as an array of object and format as JSON data – this is a more elegant method than creating a text string to build the URL

$params = @{INPUT_DATA=$inputdata;OPERATION_NAME=$operation;TECHNICIAN_KEY=$techkey;format=’json’}

# Make a web API call and record result
$response = Invoke-WebRequest -Uri $url -Method POST -Body $params

——– End of script ——-


I hope this series of blog articles has been useful. I’m finding that there are ever more customer requirements for using scripts and the API in general. I will be including more examples in future posts and also uploading them to the Forum Resources but that’s all on the ServiceDesk Plus API for now.

Enjoy!

This article is relevant to:
Service Desk

You may be interested in these other recent articles

ManageEngine ServiceDesk Plus Cloud Build Release Information

11 April 2024

Summary details of the current Build Release information for ManageEngine ServiceDesk Plus Cloud Edition. All upgrades are performed by the Zoho Cloud team. Should you…

Read more

ManageEngine Endpoint Central (formerly Desktop Central) On-Premise Build Release Information

9 April 2024

Summary details of the current Build Release information for ManageEngine Endpoint Central. Note: Desktop Central changing its name to Endpoint Central will not affect the…

Read more

ManageEngine Analytics Plus Build Release Information

4 April 2024

Summary details of the current build release information for ManageEngine Analytics Plus. Scroll the above to view more release details. Download the latest service packs…

Read more

ManageEngine OpManager Build Release Information

2 April 2024

Summary details of the current build release information for ManageEngine OpManager. Scroll the above to view more release details. Download the latest service packs here.…

Read more

ManageEngine ADSelfService Plus Build Release Information

29 March 2024

The current build release information for ManageEngine ADSelfService Plus is summarised below. Scroll down for more information. You can download the latest service packs here.…

Read more