Correct SYNTAX to create an event using the REST API and a POWERSHELL script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-18-2016 07:37 AM
Hi,
I am trying to create events using the REST API and Powershell
It works fine using the REST API explorer, but when I try to use the generated script it fails.
After tweaking it I get events with the status "error".
$body = \"source\":\"rest api explorer\"
Does anyone have the correct syntax for the above line?
Regards,
Dirk
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-18-2016 11:00 PM
I'm not sure if I completely understand what you're trying to accomplish. If it's trying to invoke a REST API from a Powershell script, you can use the code below.
Invoke-RestMethod -Uri "<YOUR REST ENDPOINT>" -Method Post -Credential $cred -Body $json -ContentType "application/json"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-19-2016 04:57 AM
Hi Mark,
Thanks for your feedback.
Let me get a bit more into detail:
I am trying to generate an event in the em_event table using the generated script from the REST API explorer. (see below)
The difficult part is the content of the body: I don't know what the correct syntax is. And I have never user powershell or the REST API before.
Kind regards,
# 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')
$headers.Add('Content-Type','application/json')
# Specify endpoint uri
$uri = "https://*************.service-now.com/api/now/table/em_event"
# Specify HTTP method
$method = "post"
# Specify request body
{request.body ? "$body = \"" :""}}{\"source\":\"rest api explorer\",\"description\":\"testing rest\",\"node\":\"Server1\",\"resource\":\"diskspace\",\"message_key\":\"low diskspace\",\"severity\":\"Major\",\"type\":\"warning\",\"alert\":\"\"}"
# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $body
# Print response
$response.RawContent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2017 01:01 AM
Hi Dirk, did you find a solution?
I have the same problem. The Powershell coming out of the REST explorer has a syntax error in the "request.body" part.
(unless they expect you to put something into the statement, which they could point out 🙂 )
Kind regards
Flemming
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2025 12:07 PM
Assuming you are using Powershell 7+ and Oauth2 credentials
