[Free webinar] 6 Ways to Return to Work with Ease using Freshservice Integrations | 10 AM PST - 9th July, 2020 Save your seat

Start a new topic

PowerShell

I was not able to find instructions for accessing the Freshservice API through PowerShell so I am providing a few examples that may benefit of other customers:


   

# Set global variables

$APIKey = 'Your 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')

# GET example: get all users (limited to first 50)

$URL = 'https://yourcompany.freshservice.com/itil/requesters.json?state=all'
Invoke-RestMethod -Method Get -Uri $URL -Headers $HTTPHeaders

# PUT example: update a user

$URL = 'https://yourcompany.freshservice.com/itil/requesters/[id].json'
$UserAttributes = @{}
$UserAttributes.Add('name', 'John Doe'
$UserAttributes.Add('phone' , '(123) 456-7890')
$UserAttributes = @{'user' = $UserAttributes}
$JSON = $UserAttributes | ConvertTo-Json 
Invoke-RestMethod -Method Put -Uri $URL -Headers $HTTPHeaders -Body $JSON

   


1 person has this question

TL;DR, solution to web exception error as of 2018 is add this to your code: 


 [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 


----


Hi John, 


Thanks for this. I have been using this without issues up until around a month ago when i started receiving the below exception:


Invoke-restmethod : The underlying connection was closed: An unexpected error occurred on a send.

At line:13 char:1

+ Invoke-restmethod -Method get -Uri $URL -Headers $HTTPHeaders -verbos ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException

    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand 


Issue is related to TLS and how Powershell presents the TLS protocols used by HTTPs. You would probably find this would impact most .NET applications. 


At the moment TLS 1.2 is the latest standard powering SSL, and anything below 1.2 is considered insecure. Its likely Freshservice API devs blocked everything except for 1.2


To ensure it works with .net framework use the following code which will force TLS 1.2 to be used by default. Put it at the start of the request above:


 [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 


Source


2 people like this

Hi ! Thank you for the info, I got this error:

Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'.


I am using this code:


 

$APIKey = "my api key"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12

$URL = "https://mydomain.freshservice.com/api/v2/tickets"
Invoke-RestMethod -Method Get -Uri $URL -Headers $HTTPHeaders @{ "X-ApiKey" = $APIKey }

 

HI Luca, try defining your HTTP headers variable prior to calling Invoke-RestMethod:

 

$HTTPHeaders = @{ "X-ApiKey" = $APIKey }
Invoke-RestMethod -Method Get -Uri $URL -Headers $HTTPHeaders

 



1 person likes this

Hello and thank you, that worked!


Now, since we are really moving our baby steps in using PowerShell with FS, my next question would be how to choose the fields I need form the tickets and apply filters, do you have examples?

https://api.freshservice.com/#introduction


loads of examples of how to GET/PUT tickets, users, agents, etc.


1 person likes this

Hi Luca,


The GET that you are performing against the tickets API endpoint will return a JSON response, so you will probably want to assign it to a variable so that you can manipulate it afterward:


$tickets = Invoke-RestMethod -Method Get -Uri $URL -Headers $HTTPHeaders

 

From there, you will be able to use additional PowerShell code to work with the $tickets variable that includes the returned data from your API request. For example, the following will output a list of all requester names from the tickets that were returned:

  

$tickets.requester_name

  

Don't forget that many API responses, including tickets, return paginated lists, so the single API call will not return all tickets.


1 person likes this

Hi Luca,


You can create a new ticket filter view with the specific conditions and use the filter views ID to invoke a API V1 call. This will help you to derive the tickets that match the filter conditions applied.


Eg :

https://yourdomain.freshservice.com/helpdesk/tickets/view/144907.json


Regards,

Sanofar


1 person likes this

Dear Sanofar (and everyone else) thank you so much, this is a whole new worled here.

I can filter the tickets in the "tickets" web page, but URL is "freshservice.com/helpdesk/tickets/filter/service_requests", where can I get the JSON file for the filter I created?

Sorry for posting again, I would need to get a json filter view for a specific type of service requests and I will need to see the fields of the service requests.

I could create a report for this, but the report is sending me an email with the data, this is not the way I wanna go.

I need either a Data Export API link or the JSON filter view so I can query it directly with PowerBI

Dear Luca,


Currently we do not support the data export for Service items through Analytics. Our team is working on this, will update the thread with an ETA post discussing this with our Product team.


Cheers!

Login or Signup to post a comment