Powershell example for createing an ITOM Event

Nigel S
Kilo Contributor

Is there an exanple for using PowerShell to create an ITOM event using the API via  a Mid Server?

1 ACCEPTED SOLUTION

JF Muller
ServiceNow Employee
ServiceNow Employee

Hi Nigel,

Please find below an example:

function Get-BasicAuthCreds {
    param([string]$Username,[string]$Password)
    $AuthString = "{0}:{1}" -f $Username,$Password
    $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
    return [Convert]::ToBase64String($AuthBytes)
}
$BasicCreds = Get-BasicAuthCreds -Username "evt.integration" -Password "ServiceN0w!"
$body = '{	"records":	
[
     {
     "source":"SCOM",
     "event_class":"SCOM 2007 on scom.server.com",
     "resource":"C:",
     "node":"name.of.node.com",
     "metric_name":"Percentage Logical Disk Free Space",
     "type":"Disk space",
     "severity":"4",
     "description":"The disk C: on computer V-W2K8-dfg.dfg.com is running out of disk space. The value that exceeded the threshold is 41% free space.",
     "additional_info":""
      }     
   ]
}'
Invoke-WebRequest -Uri "http://localhost:9082/api/mid/em/jsonv2" -Headers @{"Authorization"="Basic $BasicCreds"} -ContentType "application/json" -Method Post -Body $body

View solution in original post

8 REPLIES 8

robertgeen
Tera Guru

Nigel,

I have an example somewhere but have to track it down. I'll try to find it by end of day and post an example here. Thanks.

JF Muller
ServiceNow Employee
ServiceNow Employee

Hi Nigel,

Please find below an example:

function Get-BasicAuthCreds {
    param([string]$Username,[string]$Password)
    $AuthString = "{0}:{1}" -f $Username,$Password
    $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
    return [Convert]::ToBase64String($AuthBytes)
}
$BasicCreds = Get-BasicAuthCreds -Username "evt.integration" -Password "ServiceN0w!"
$body = '{	"records":	
[
     {
     "source":"SCOM",
     "event_class":"SCOM 2007 on scom.server.com",
     "resource":"C:",
     "node":"name.of.node.com",
     "metric_name":"Percentage Logical Disk Free Space",
     "type":"Disk space",
     "severity":"4",
     "description":"The disk C: on computer V-W2K8-dfg.dfg.com is running out of disk space. The value that exceeded the threshold is 41% free space.",
     "additional_info":""
      }     
   ]
}'
Invoke-WebRequest -Uri "http://localhost:9082/api/mid/em/jsonv2" -Headers @{"Authorization"="Basic $BasicCreds"} -ContentType "application/json" -Method Post -Body $body

Thanks for this.

natasciaheil
Kilo Guru

I would recommend to directly write the event with the Table api into the event table:

# Eg. User name="admin", Password="admin" for this code sample.
$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://xxx.service-now.com/api/now/table/em_event"

# Specify HTTP method
$method = "post"

# Specify request body
{request.body ? "$body = \"" :""}}{\"source\":\"SCOM\",\"event_class\":\"SCOM 2007 on scom.server.com\",\"resource\":\"C:\",\"node\":\"name.of.node.com\",\"metric_name\":\"Percentage Logical Disk Free Space\",\"type\":\"Disk space\",\"severity\":\"4\",\"description\":\"The disk C: on computer V-W2K8-dfg.dfg.com is running out of disk space. The value that exceeded the threshold is 41% free space.\"}"

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $body

# Print response
$response.RawContent

Regards,

Natascia