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

Hi natasciaheil,

No.. This is a bad practice when it comes to Event Management, you should either:

a. Post event against the instance Event Management web service API

https://<instancename>.service-now.com/api/global/em/jsonv2

b. Post event against MID WebService Event Collector

http://{MID_Server_IP}:{MID_Web_Server_Port}/api/mid/em/jsonv2

Regards

Jef

 

Thanks for this clarification.

Nat

Jef,

Quick question for you why is it best practice to use that API? What makes it any different then going directly to the event table? I'm not saying you are wrong I am just curious the reason for this as I've never been able to get that answered by someone. Thanks.

Nigel S
Kilo Contributor

Thanks for the response. Is there an example using API Key Credentials?