Create Incident with Powershell

JamesLindsay
Giga Guru

I am looking for some advice on how to solve an issue I am having opening an incident using the REST API at /api/now/v1/table/incident. I have a PowerShell script that I am using to integrate with a monitoring source that will execute the script as an action to open an incident. What I am finding is that when I pass the JSON payload using:
$response = Invoke-RestMethod -Uri $ApiUrl -Method Post -Headers $headers -Body $CaseDetailsJson
The ticket that is created does not have several of the passed field values populated. Specifically, the "impact" field is passed the wrong value and the "assignment_group" and "description" fields are left empty. The very same payload pasted into a PostMan session works perfectly fine.

I build out the JSON with this:

$CaseDetails = @{
"impact" = $impact
"urgency" = $urgency
"cmdb_ci" = $cmdb_ci
"business_service" = $business_service
"u_categorization" = $u_categorization
"short_description" = $short_description
"description" = $description
"contact_type" = $contact_type
"assignment_group" = $assignment_group
}

$CaseDetailsJson = $CaseDetails | ConvertTo-Json -Compress

 

The headers used are:
$headers = @{
"Authorization" = "Basic $Credentials"
"Accept" = "application/json"
"Content-Type" = "application/json"
}

At this point I'm still keeping it very simple and testing with some very basic values knowing that from my monitoring event source, the description field could get complicated. As I said, I can take the exact same JSON produced from this script that I wrote to a text file for checking, paste it into Postman and I get exactly what I expect in the ticket. I am at a complete loss at why it snot working from PowerShell. Thank you for any insight or help you may provide.

1 ACCEPTED SOLUTION

JamesLindsay
Giga Guru

At this point what I have found is that I was attempting the open the incident with a User in ServiceNow that did not have the ITIL role but, the user I was using in Postman did. Not sure why it took so long for me to see that delta. After adding the role it now works as designed. However, I am hoping to avoid the cost of another ITIL user license so I've started looking at what it might take to insert the record into an Import Set table instead.

View solution in original post

2 REPLIES 2

Rakesh18081
Tera Expert

It sounds like PowerShell isn't correctly formatting or transmitting the JSON payload as expected. Since the same payload works in Postman, here are a few things to check:
1. Ensure Proper JSON Formatting
PowerShell's ConvertTo-Json can sometimes alter the structure of the payload. Try using -Depth 10 to ensure nested objects are correctly formatted:
$CaseDetailsJson = $CaseDetails | ConvertTo-Json -Depth 10 -Compress


2. Verify Encoding
PowerShell may encode special characters differently than Postman. Try explicitly setting UTF-8 encoding:
$utf8Bytes = [System.Text.Encoding]::UTF8.GetBytes($CaseDetailsJson)
$response = Invoke-RestMethod -Uri $ApiUrl -Method Post -Headers $headers -Body $utf8Bytes -ContentType "application/json"


Log the headers of both postman and PowerShell and compare to see if postman is passing additional headers which are not present in PowerShell .

JamesLindsay
Giga Guru

At this point what I have found is that I was attempting the open the incident with a User in ServiceNow that did not have the ITIL role but, the user I was using in Postman did. Not sure why it took so long for me to see that delta. After adding the role it now works as designed. However, I am hoping to avoid the cost of another ITIL user license so I've started looking at what it might take to insert the record into an Import Set table instead.