Contents

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

Creating Incidents in Servicenow using Powershell and Python

Views

Usually i am not very enthusiastic about Servicenow ITSM but I do anything I can do to make my team and people’s life easier whille working with this big IT tool, we cannot deny the fact Servicenow is one of greatest IT enterprise tool which is responsible for providing technical management support, such as IT service management for the major companies around the globe.

Below I’m showing some simple manners to create Servicenow Incidents using JSONv2.

Both scripts can be found in the following Github repository folder servicenow/Incidents.

Creating Incidents in Servicenow using Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$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