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.

What is ServiceNow?

ServiceNow is a cloud-based platform that provides enterprise-level IT service management (ITSM) solutions. It is designed to automate and streamline IT workflows, such as incident management, problem management, change management, and service request management.

ServiceNow allows IT organizations to centralize and manage their IT services through a single, integrated platform. It includes a wide range of features and modules, such as service catalog, knowledge management, asset management, and configuration management.

One of the key benefits of ServiceNow is its ability to improve efficiency and productivity by automating manual tasks and workflows. It also provides real-time visibility into IT operations, allowing organizations to monitor and track the performance of their IT services.

In addition to ITSM, ServiceNow also offers solutions for other areas of the enterprise, such as HR, finance, and security operations. It can be customized and configured to meet the specific needs of different organizations, and integrates with other popular enterprise systems and tools.

Overall, ServiceNow is a powerful tool for organizations that want to streamline their IT operations and improve the delivery of IT services to their users.

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
24
 data-state="open" data-collapse="false"
$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