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
‎09-30-2020 12:04 PM
As you said, 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.
Can you please share the script for the above , it would be very helpful for my project
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-13-2022 09:10 AM
I get an XML result payload when i open this link (replacing the domain with ours)
https://mytestinstance.service-now.com/api/sn_sc/servicecatalog/items
it shows multiple SYS_IDs. The script above is able to create REQ with Only 2 out of 9, the rest of the SYS_IDs return an error
Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At line:71 char:13
+ $response = Invoke-WebRequest -Headers $headers -Method $method -Uri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Does anyone know why it works on some sys_ids and not others?