Powershell Examples and Scripts to interact with ServiceNow REST APIs
Splatting is used for improved readability.
Issue a GET request to the Table API
Parameters:
- InstanceName
- Table
- Query (sysparm_query)
- DisplayValue (sysparm_display_value)
- ExcludeRefLink (sysparm_exclude_reference_link)
- Fields (sysparm_fields)
- Limit (sysparm_limit)
- View (sysparm_view)
$Params = @{
InstanceName = "development"
Table = "change_request"
Query = "active=true"
Fields = "number,short_description,state,assigned_to"
Limit = 5
}
.\Get-TableAPI.ps1 @Params
$Params = @{
Method = "Post"
Uri = "[...]/file?table_name=sc_req_item&table_sys_id=abc123&file_name=test.txt"
ContentType = "multipart/formdata"
InFile = "test.txt"
}
Invoke-WebRequest @Params
$Params = @{
Method = "Get"
Uri = ".../api/now/attachment/40f93e17"
}
$Response = Invoke-WebRequest @Params
$Meta = $ResponseMeta.Content |
ConvertFrom-Json |
Select-Object -ExpandProperty result
$Meta
# size_bytes : 5120
# file_name : incident.xls
# download_link : https://dev.sn.com/api/now/attachment/123.../file
# content_type : application/vnd.ms-excel
# .
# .
# .
Gets the content of an attachment.
The x-attachment-metadata
header contains the same data that is returned by a request to api/now/attachment/{sys_id}
$Params = @{
Method = "Get"
Uri = ".../api/now/attachment/40f93e17/file"
}
$Response = Invoke-WebRequest @Params
$Meta = $Response.Headers.'x-attachment-metadata' |
ConvertFrom-Json
# write content to file
[System.IO.File]::WriteAllBytes($Meta.file_name, $Response.Content)