childebrandt42

Worthless info……………Oh blinky lights!

Service Now Incident Creation and Updating with PowerShell

Posted by

·

, , ,

For those of you that are cursed or blessed by the use of ServiceNow, I have been working on a project to automate a ton of my day to day job. Part of this automation of the service Incidents. I spent a few painful hours messing around with the ServiceNow rest API’s. Keep in mind I am no developer and don’t know Rest but after enough trial SNDevand error, I managed to get something out of it. I thought it would be worth the effort to save you all some pain and anguish.

There are a few things you will need to make this productive. You will need to know the Sys_ID of your ServiceNow account, the Sys_ID of the Assignment Group and Service Offering. Also, remember that this also depends on your Incident Form and what your company has customized. For us, we have a few required fields so I had to make sure those were filled.

Finding Sys_IDs is easy once you figure out they are a 32 character code listed in the URL of the objects you are looking for. Same with your Incident’s.

First Open an existing incident with your name in it. On the right of your name is the “i” Click on it and then the “Open Record” button on the upper right-hand side. ChrisHil

This will open your User record in ServiceNow. In the URL you will see your Sys_ID. ChrisHSys_ID

You can do this for the rest of the other Sys_ID’s you need.

From there you just need to gather all the info that is required or you would like to use.

This is a basic script to fill out all the fields I needed for my use case. In this script, you need to edit the username and password. You can use encrypted if you like. You will need your ServiceNow URL and of course all your Sys_ID’s and info from the fields that have drop-down menus.

# Create New ServiceNow Incident
# Chris Hildebrandt
# 10-26-2018
# Ver 1.0
# Script will create a new ServiceNow Incident via Powershell
#______________________________________________________________________________________

#______________________________________________________________________________________
#ServiceNow creds
$user = "admin"
$pass = "admin"

# 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://YOURSERVICENOW.service-now.com/api/now/table/incident"

# Specify HTTP method
$method = "post"

# Specify request body
$body = @{ #Create Body of the Post Request
    caller_id= "29711cc64f2bd308feda99701310c727"
    urgency= "2"
    impact= "3"
    priority= "4"
    contact_type= "email"
    notify= "2"
    watch_list= "29711cc64f2bd308feda99701310c727"
    service_offering= "Service Offering 32 Char Sys_ID"
    u_production_impact= "No"
    category= "Your Catagory Find from the drop down menu"
    subcategory= "Find the subcategory from the list"
    u_item= "request"   #Check the Drop down for the options
    assignment_group= "Assignment Group 32 Char Sys_ID"
    assigned_to= "29711cc64f2bd308feda99701310c727"
    short_description= "Short Description of the Incident"
    description= "Description of the Incident"
    work_notes= "Work Notes"
    comments= "Additional Comments and Notes"

}
$bodyjson = $body | ConvertTo-Json

# Send HTTP request
$CreateServiceIncident = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $bodyjson -ContentType "application/json"

# Print response
$CreateServiceIncident.RawContent

 

This script will update an existing ServiceNow Incident. You will need the Sys_ID for the incident you are updating. That can be found in the URL of the incident.

Sys_ID

# Update Existing ServiceNow Incident

# Chris Hildebrandt

# 10-26-2018

# Ver 1.0

# Script will update existing ServiceNow Incident via Powershell

#______________________________________________________________________________________

#______________________________________________________________________________________

#ServiceNow creds

$user = "admin"

$pass = "admin"

# 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://YOURSERVICENOW.service-now.com/api/now/table/incident/YourINCIDENTSys_ID"

# Specify HTTP method

$method = "patch"

# Specify request body

$body = @{ #Create Body of the Post Request

work_notes="Update Work Notes"

close_notes="Your Close Notes"

}

$bodyjson = $body | ConvertTo-Json

# Send HTTP request

$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $bodyjson -ContentType "application/json"

# Print response

$response.RawContent

 

This script you can add other fields if you like. For me, I was using Work Notes and Close Notes.

The Links for these Scripts are on my GitHub repo HERE. Along with a few other Scripts.

childebrandt42 Avatar

About the author

Hi! My name is Chris Hildebrandt, I’m a EUC consultant by day, and automation junkie at night. I have worked my way from being small customer to enterprise side customer, with a recent transition into consulting. I enjoy everything EUC focused and more so on how to automate it. I like to find ways to make mine and your jobs easier with automation.