logo

NJP

Powershell -> Read ServiceNow data via REST API

Import · Nov 11, 2020 · article

Reference : http://www.john-james-andersen.com/blog/service-now/powershell-probe-and-utility-for-servicenow.html

https://arindamhazra.com/service-now-incident-management-using-powershell/

Sample script to print raw output

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

$pass = "Password"

# 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://.service-now.com/api/now/table/incident?sysparm_limit=1"

# Specify HTTP method

$method = "get"

# Send HTTP request#$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri

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

# Print response

Write-Output $response.RawContent

In order to export the content to csv

# Print responseWrite-Output $response.RawContent$responseJSON = $response | ConvertFrom-JsonWrite-Output $responseJSON.result

$responseJSON.result | ConvertTo-Csv -NoTypeInformation | Out-File -FilePath D:\AccessDB\test.csv

Labels:

image

View original source

https://www.servicenow.com/community/now-platform-articles/powershell-gt-read-servicenow-data-via-rest-api/ta-p/2318569