Use PowerShell With ServiceNow API – How To Authenticate and Return Ticket Data

ServiceNow is one of the leading ITSM, ITOM & ITBM platforms on the market. The organisations I work for use it extensively for their daily ticketing system and all IT related service requests… amongst other things. For me, it provides an opportunity to automate the insertion of Microsoft related communications within it.

The platform offers an API and this can be utilised in many ways although I tend to mostly write PowerShell runbooks in Azure Automation and log/audit tickets in this way. I do also use the Power Automate ServiceNow connector when the task is more suitable to that platform, perhaps when using data from Common Data Service or Dataverse.

In this blog post, I will take you through some simple steps using basic authentication to connect to the ServiceNow API using PowerShell. I will then search for a ticket and return data linked to that ticket.

You may find it helpful to create your own ServiceNow Developer Instance, a playground if you will. If that is the path you want to take then please see my other post titled "ServiceNow – Create Developer Instance". Once you have your ServiceNow dev instance, you may choose to work and elevate your recently created admin account or if you wish, follow the steps that I take and create and elevate your first admin user.

NB: I have used a plain text username & password in this example. I don't advocate doing this in production and this is just a demo, so please be aware of that and make sure a secure method is used when using basic authentication or consider using an alternative Oauth approach.

ServiceNow Table and Record Information

Just to set the scene with my example, it's important to understand the format of the endpoint we will be making our GET request call to.

If you have a ServiceNow Instance called contoso, you would send your GET request of to:

The ServiceNow test instance I am using has a custom table for "New Requirements". These New Requirements cater for a variety of logged service requests and in this example, one of Microsoft's Message Centre Notices has been logged here utilising a custom "External Reference" field as you will see in the below image:

The table name within the ServiceNow meta data is actually u_new_requirement, this means the API endpoint will be …

So whilst I am using some custom fields here, you can target any table, record or value with this method. So let's get to it, let's have a look at the code and you can try it for yourself.

# Set the credentials
$User = 'contoso.adminstrator'
$Pass = 'P4ssw0rd!!'

# Set headers
$GlobalHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$GlobalHeaders.Add('Accept', 'application/json')
$GlobalHeaders.Add('Content-Type', 'application/json')

# Build & set authentication header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Pass)))
$GlobalHeaders.Add('Authorization', ('Basic {0}' -f $base64AuthInfo))

# Load variable with ServiceNow instance URL
$ServiceNowInstance = ''

# Build the request body
$Body = @{
    u_external_ref = 'MC242578'

# Build the paramaters that will be sent off with the REST call
$Params = @{
    Headers = $GlobalHeaders
    Method  = "GET"
    Uri     = "$ServiceNowInstance/api/now/table/u_new_requirement"
    Body    = $Body

# Make the REST GET request
$Response = (Invoke-RestMethod @Params).Result

# Filter the response to show values that match the image ..
$Response | Select-Object number, u_external_ref, description

The response I get when I run the code above looks like the screen shot below:

You will see that the data in the response matches the values that appear in the image. This shows that the response is accurate and I welcome you to give it a go. Change the table that you are sending your requests to, filter on diferent values, the opportunities just keep presenting themselves when using PowerShell with ServiceNow.

Thanks for reading, Alan.