Contents

How to Create a Incident in Servicenow Using Servicenow API (Powershell and Python)

Below I’m sharing some simple methods to create Servicenow Incidents using JSONv2. Both scripts can be found in the following Github repository folder iservicenow/Incidents.

Creating Incidents in Servicenow using Python

import json
import requests
from requests.auth import HTTPBasicAuth

auth = HTTPBasicAuth("youruser@yourcompany.com", "Y#urPwd1234")
uri = "https://demo.service-now.com/incident.do?JSONv2"

headers = {
    "Accept": "application/json;charset=utf-8",
    "Content-Type": "application/json"
}

# define payload for request, note we are passing the sysparm_action variable in the body of the request
payload = {
    "sysparm_action": "insert",
    "category": "Infrastructure",
    "impact": "1",
    "urgency": "2",
    "short_description": "Automated ticket Short Description",
    "description": "Automated ticket Description",
    "cmdb_ci": "Email",
    "caller_id": "Allan Schwantd",
    "contact_type": "Email",
    "company_name": "CMPv2",
}

r = requests.post(url=uri, data=json.dumps(payload), auth=auth, verify=False, headers=headers)
content = r.json()
assert (r.status_code == 200)
print ("Response Status Code: " + str(r.status_code))
print ("Response JSON Content: " + str(content))

Creating Incidents in Servicenow using Powershell

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "youruser@yourcompany.com", "Y#urPwd1234")))
$headers.Add("Authorization", "Basic $base64AuthInfo")
$headers.Add("Content-Type", "application/json")

# define payload for request, note we are passing the sysparm_action variable in the body of the request
$body = @"
{
	"sysparm_action": "insert",
	"category": "Software",
	"impact": "1",
	"urgency": "2",
	"short_description": "Automated ticket Short Description",
	"description": "Automated ticket Description",
	"cmdb_ci": "Email",
	"caller_id": "Arnold Schwarzenegger",
	"contact_type": "Email",
	"company_name": "CompanyName"
}
"@

$response = Invoke-RestMethod 'https://myservicenowinstance.service-now.com/incident.do?JSONv2' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json