Create ServiceNow Request through Powershell via REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2018 05:30 PM
Hi ServiceNow Community,
This is my first posted question, but I have used the community resources for some time now. I've always been able to find the answer to my question if not here then on other forums, but after digging around for a few days now and trying tons of variations to my code I can't seem to figure this one out. I'm running into an issue using powershell to POST a Request to the service catalog in ServiceNow through the REST Api. Receiving error: "Mandatory Variables are required".
BACKGROUND: (1) I have been able to successfully POST an Incident to ServiceNow using the same powershell script only altering the endpoint uri and request body. (2) I have also been able to successfully POST a request to the specific service catalog item via the REST API Explorer as well as using Fiddler. (3) I have double checked and the only mandatory variables for the specific service catalog item are (requested_for) and (Request_Type) both of which are defined in the powershell script body.
Here is my powershell code. Any help or suggestions are greatly appreciated!
# Eg. User name="admin", Password="admin" for this code sample.
$user = "myusername"
$pass = "mypass"
# 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://mytestinstance.service-now.com/api/sn_sc/servicecatalog/items/3a05b357db2443009906540adc961945/order_now"
# Specify HTTP method
$method = "Post"
# Specify request body
$body = @{
'sysparm_quantity' = '1'
'requested_for' = 'Guest'
'Request_Type' = 'New Account'
'website_login' = '2437ffdbdb2443009906540adc96198c'
}
$bodyJson = $body | ConvertTo-Json
#Convert to UTF8 array to support special chars such as the danish "�","�","�"
$utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($bodyJson)
# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $utf8Bytes -ContentType "application/json" -UseBasicParsing
$response.RawContent

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2018 09:44 PM
hi there,
have u tried using the built in "rest api explorer" and only fill up the information like below and see is the "Mandatory Variables are required"?
'sysparm_quantity' = '1'
'requested_for' = 'Guest'
'Request_Type' = 'New Account'
'website_login' = '2437ffdbdb2443009906540adc96198c'
Once testing is done (assuming u need to add in more input params), u can generate relevant powershell script.
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2018 10:29 PM
I didn't test anything, but one thing that stands out to me is your URL doesn't match what I see in the documentaion. I believe it should be
https://mytestinstance.service-now.com/api/sn_sc/servicecatalog/cart/submit_order
Also, not sure if the UTF8 conversion at the end is necessary. You may also want to try using the PowerShell credential object instead of including authorization in the header with Base64, although either should work. Good luck!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2018 12:16 PM
Thank you both for your responses, I really appreciate the help. I figured out the issue was that I was not defining what are variables in my json body. Once I added in a 'variables' parameter in the body it worked like a charm.
$body = @{
'sysparm_quantity' = '1'
'variables' = @{
'requested_for' = "$RequestedFor"
'Request_Type' = $RequestType
'website_login' = $Vendor1
}}
Also, as Chris Sanford suggested the UTF8 conversion was not necessary so I removed that line of code and as a result, updated the body variable at the end.
-Body $Bodyjson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2019 09:03 PM
can you please share your incident powershell script