Powershell Script Action to Post into ServiceNow Table (PowerShell)

Rather than use the baked in ServiceNow Incident Creation Action we wanted to post our potential incidents into a staging table within ServiceNow, rather than straight into the incidents table.

 

The following rough and ready powershell achieves this by leveraging the ServiceNow API via PowerShell. This assumes you know how to create a custom table and permissions in ServiceNow. Some of the formatting required for the WUG percent variables is a bit funky.

  1. # Action Name : ServiceNow - Staging Table
  2. # Author : Graham Pritchard
  3. # Date : 06/02/2020
  4. # Description : Powershell Script Action to POST into ServiceNow staging table
  5. # ----------------------------------------------------------------------------------
  6.  
  7. # Specify endpoint uri
  8. $uri = "https://yourinstancename.service-now.com/api/yourfolder/yourtablename"
  9.  
  10. # Basic Authentication credentials
  11. $user = "yourservicenowusername"
  12. $pass = "yourservicenowpassword"
  13.  
  14. # Build auth header
  15. $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
  16.  
  17. # Set proper headers
  18. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  19. $headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
  20. $headers.Add('Accept','application/json')
  21. $headers.Add('Content-Type','application/json')
  22.  
  23. # Specify HTTP method
  24. $method = "post"
  25.  
  26. # ---- start description ----
  27. $descriptionString = @"
  28. %Device.DisplayName is %Device.State.
  29.  
  30. Monitors down include:
  31. %Device.ActiveMonitorDownNames
  32.  
  33. Brand:
  34. %Device.Attribute.Brand
  35. %Device.Attribute.Vendor
  36.  
  37. Model:
  38. %Device.Attribute.Model
  39.  
  40. Device Description:
  41. %Device.Attribute.Description
  42. "@
  43. # ---- end decription ----
  44.  
  45. $deviceModel = @"
  46. %Device.Attribute.Model
  47. "@
  48.  
  49. $deviceMAC = @"
  50. %Device.Attribute.MACAddress
  51. "@
  52.  
  53. $JSON = ConvertTo-Json @{
  54. alert_severity = "Exception"
  55. short_description = "EXCEPTION: Monitors down for %Device.DisplayName"
  56. description = $descriptionString
  57. ip_address = "%Device.Address"
  58. mac_address = $deviceMAC
  59. device_name = "%Device.DisplayName"
  60. device_type = "%Device.Role"
  61. device_model = $deviceModel
  62. service_owner = "Network & Data Centre Services"
  63. device_link = "https://yourwugserver:443/NmConsole/#v=Wug_view_dashboard_DeviceStatus/p=%7B%22deviceId%22%3A%Device.DatabaseID%7D"
  64. }
  65.  
  66. $Response = Invoke-WebRequest -Uri $uri -Headers $headers -Method $method -Body $JSON -UseBasicParsing
  67.  
  68. # Use available context variables as needed
  69. $resultjson = convertfrom-json $Response.Content
  70. $resultText = $resultjson.result.message
  71.  
  72. if($Response.StatusCode -eq 200)
  73. {
  74. $actionResult = 0;
  75. $resultText = "ServiceNow " + $resultText
  76. }
  77. else
  78. {
  79. $actionResult = 1;
  80. $resultText = "Error- row not added: $( $resultText )"
  81. }
  82. # Set the result
  83.  
  84. $Context.SetResult($actionResult, $resultText)

The posts received in the ServiceNow look like this:

WUG_to_ServiceNow01

WUG_to_ServiceNow02

And the post confirmation is displayed in the action log.

 

WUG_to_ServiceNow03