Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

PowerShell script to create an incident using REST API

Sujit Karn
Tera Contributor

# Define ServiceNow instance details
$instance = "https://dev080088.service-now.com"
$username = "itil.user"
$password = "password"
$table = "incident"

 

# Create the REST API endpoint
$uri = "$instance/api/now/table/$table"

 

# Create the incident payload
$body = @{
short_description = "4th Sep incident from PowerShell"
urgency = "2" # 1 - High, 2 - Medium, 3 - Low
impact = "2"
caller_id = "System Administrator" # Replace with a valid caller ID
} | ConvertTo-Json -Depth 3

 

# Create headers
$headers = @{
"Content-Type" = "application/json"
}

 

# Create credentials
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($username, $securePassword)

 

# Submit the incident
try {
$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body -Credential $creds
} catch {
Write-Error "API call failed: $_"
}

 

# Output the result
$response.result | Format-List

0 REPLIES 0