Powershell ServiceNow API

David Vu1
Kilo Expert

What should i do with the
{request.body ? "$body = \"" :""}}


Because it show the errors when execute in Powershell script?

Thanks,

find_real_file.png

# Eg. User name="admin", Password="admin" for this code sample.

$user = "admin"

$pass = "admin"

# Build auth header

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))

$headers.Add('Accept','application/json')

# Specify endpoint uri

$uri = "https://devxxxxxservice-now.com/api/now/table/dms_document/e3aec2250a0a2c40098e82adfc4252a0"

# Specify HTTP method

$method = "get"

{request.body ? "$body = \"" :""}}

# Send HTTP request

$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri

# Print response

$response.RawContent

1 ACCEPTED SOLUTION

JBrennan
Giga Contributor

I've just had a few minutes to put together some screen shots to further troubleshoot and think I have fixed the problem (or at least been able to do want I need).



First things first, lets acknowledge the Service-Now developers have screwed up this line of PowerShell {request.body ? "$body = \"" :""}} and hope they go back and fix it at some stage. I'm no PowerShell expert but assume they are trying to use regex to format the body. The poor mans fix for this is to add the $body = @ { name="blar" } as seen in this post PowerShell Explore REST Api   but it looks like you have to specify each field you want to return (which the regex would eliminate).



HOWEVER, I have been testing with the Invoke-RestMethod command instead of the Invoke-WebRequest and am able to return each field as a PowerShell object i.e. $response.<fieldname> instead of one big blob of text.



Hope it helps someone else


find_real_file.png



find_real_file.png


View solution in original post

8 REPLIES 8

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Hao,


Could you post screenshot of the Api Explorer screen where the get is defined?


This will help investigate the issue.



If the reply was informational, please like, mark as helpful or mark as correct!


David Vu1
Kilo Expert

Thanks,
find_real_file.png


Hi Hao,


Could you try a Retrieve a record (GET) and specify a sys_id - for an existing record in the target?


It could be the empty sysparm_query is problematic.


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Hao,


Could you also advise what errors you see when invoking the powershell script?


If the reply was informational, please like, mark as helpful or mark as correct!