Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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