Question

How to create a Service Request Using a PowerShell Script?

  • 23 July 2022
  • 4 replies
  • 824 views

Background: So our HR team puts a csv on a fileshare with a list of users that have left our organisation. I have a powershell script that checks that share every hour and will only run if there’s a new file. The PowerShell Script handles all the offboarding of use but we’re still required to log the ticket manually on Freshservice.

Unfortunately, my API knowledge is very limited and I have no idea how to even get started on this. I’ve had a look at the documentation but can’t see how to implement this into PowerShell.

Ideally, this should all be done in PowerShell with no 3rd party programs.

This is what I’ve done so far:

  1. Typed in the below on a command prompt in Windows  

 

curl -v -u myapi:X -H "Content-Type: application/json" -X GET "https://mydomain.freshservice.com/api/v2/tickets/randomticketnumber"
  1. Grabbed the “ Authorization: Basic” value that came up in the output. I used this because for some reason, Postman doesn’t seem to work with the API key even if I set the value to X.

 

  1. Grabbed the below information from Postman as an example of an existing Leavers Service Request Item that i would want the  script to create.
{
"requested_items": [
{
"custom_fields": {
"type": "Leaver List",
"prior_to_which_date": null,
"leavers_name": 12345678901,
"leave_date": "2022-05-30"
},
"id": 26001158185,
"created_at": "2022-05-24T10:50:22Z",
"updated_at": "2022-05-24T10:50:22Z",
"quantity": 1,
"stage": 1,
"loaned": false,
"cost_per_request": 0.0,
"remarks": null,
"delivery_time": null,
"is_parent": true,
"service_item_id": 86
}
]
}
  1. For what it’s worth, here’s the output of the ticket itself:
{
"ticket": {
"cc_emails": [],
"fwd_emails": [],
"reply_cc_emails": [],
"bcc_emails": null,
"fr_escalated": true,
"spam": false,
"email_config_id": null,
"group_id": 10987654321,
"priority": 2,
"requester_id": 12345678901,
"requested_for_id": 12345678901,
"responder_id": 12345678901,
"source": 2,
"status": 2,
"subject": "Leaver - John Smith - 2022-05-30 00:00:00 Leaver List",
"to_emails": null,
"sla_policy_id": 26000004243,
"department_id": 26000071744,
"id": 12345,
"type": "Service Request",
"due_by": "2022-05-27T07:50:27Z",
"fr_due_by": "2022-05-25T09:50:27Z",
"is_escalated": true,
"description": "",
"description_text": "",
"custom_fields": {
"closing_subcategories": null,
"contact_number_not_required": null,
},
"created_at": "2022-05-24T10:50:22Z",
"updated_at": "2022-05-24T11:06:27Z",
"urgency": 1,
"impact": 1,
"category": "Service Requests (Don't use for Incidents)",
"sub_category": null,
"item_category": null,
"deleted": false,
"attachments": [],
"approval_status": 4,
"approval_status_name": "Not Requested"
}
}

 

 

Problem is, I have no idea how to even get started on sending the request from PowerShell. More to the matter, how do I use the UPN of the user (Fresh is linked to our Active Directory) to populate the “leavers_name” field or do I have to use the unique number for each person on Fresh?

Has anyone managed to implement something similar in their own environment?


4 replies

Userlevel 7
Badge +16

Hello @GavinEldred you will want to make sure that you are using Basic Auth in Postman if you are making a request. Pass the API key in for username, and a “x” for the password. That should help you test in Postman.

This is completely doable in powershell, and you can even automate the process through a Freshservice Workflow Automator in conjunction with the Orchestration Server and Powershell Application. 

Depending on what fields you have in the record coming from the fileshare, you can use GET requests via powershell to find the requester’s email address and then pass that value as the requester in the body of your POST Request.

Here are a couple resources

https://support.freshservice.com/en/support/solutions/folders/50000000533

https://support.freshservice.com/en/support/solutions/articles/50000003610-installation-guide-powershell

Hopefully that gets you started. Let us know if you need any more help! Take care.

Badge +1
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 
$APIKey = '<API Key>'
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $APIKey,$null)))
$HTTPHeaders = @{}
$HTTPHeaders.Add('Authorization', ("Basic {0}" -f $EncodedCredentials))
$HTTPHeaders.Add('Content-Type', 'application/json')


$TicketURL = 'https://<Instance>.freshservice.com/api/v2/tickets/'

$Body = <JSON to create new ticket>

Invoke-RestMethod -Method Post -Uri $TicketURL -Headers $HTTPHeaders -Body $Body

Hi @GavinEldred, Like Zachary put, its totally possible to do this using the workflow automator too.

The above might help you get started, this is extract from what I use for doing something similar, you just need to update the API key, TicketURL with your instance name and use the body as a json to submit the request for each leaver (probably in a loop in PS). 

If its just a CSV file, there may be better ways to achieve this like a service item to initiate the request but appreciate its not always that simple to change a process.

Userlevel 3
Badge +2

...how do I use the UPN of the user (Fresh is linked to our Active Directory) to populate the “leavers_name” field or do I have to use the unique number for each person on Fresh?

 

 

Just thought I’d make the point that Fresh doesn’t use the User Principal Name, but an email address - we have an instance with a user whose UPN is different from their email, and Fresh uses his email address.

Hello @GavinEldred you will want to make sure that you are using Basic Auth in Postman if you are making a request. Pass the API key in for username, and a “x” for the password. That should help you test in Postman.

This is completely doable in powershell, and you can even automate the process through a Freshservice Workflow Automator in conjunction with the Orchestration Server and Powershell Application. 

Depending on what fields you have in the record coming from the fileshare, you can use GET requests via powershell to find the requester’s email address and then pass that value as the requester in the body of your POST Request.

Here are a couple resources

https://support.freshservice.com/en/support/solutions/folders/50000000533

https://support.freshservice.com/en/support/solutions/articles/50000003610-installation-guide-powershell

Hopefully that gets you started. Let us know if you need any more help! Take care.

thanks dear...

Reply