
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 11:06 PM
What should i do with the
{request.body ? "$body = \"" :""}}
Because it show the errors when execute in Powershell script?
Thanks,
# 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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2017 09:08 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2017 09:08 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-21-2017 10:09 PM
Hi Jeremy,
Thank you very much for your posts above - very informative!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2017 12:12 AM
Sorry, One last thing on thing: Again I'm not a PowerShell expert and I'm sure somebody with better skills than I can probably suggest a better way of doing it and/or explain further but I have figured out how to continue to use Invoke-WebRequest but format the output to allow you to reference each field as a PowerShell object (see below ##############)
#Service-Now code sample
# 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://xxxxxxxxxxxx.service-now.com/api/now/table/cmdb_ci_server?sysparm_limit=1"
# Specify HTTP method
$method = "get"
#SERVICE_NOW TO FIX THE BELOW LINE
#{request.body ? "$body = \"" :""}}
# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri
# Print response
$response.RawContent
####################JB's Edit#####################################
# Convert response from json
$response2 = $response.Content | ConvertFrom-Json
# Now you can select specific fields as PowerShell Objects. Example:
$response2.result | Select name, os, classification
#Example of adding specific field to a string
$response2.result | % {Write-host "The name of the server is: $($_.name)"}
#Example of If statement
$response2.result | % {IF ($_.name -like "*Sacramento*") {Write-host $_.name}}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2018 06:12 AM
logged a ticket with ServiceNow support... This issue is caused by PRB656768. Check the KB and the workaround listed: https://hi.service-now.com/kb_view.do?sysparm_article=KB0597958
Here is their workaround...
Bad Line: {request.body ? "$body = \"" :""}}
Good Line: {request.body ? "$body="}