
Community Archive (Customer) asked a question.
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.
- # Action Name : ServiceNow - Staging Table
- # Author : Graham Pritchard
- # Date : 06/02/2020
- # Description : Powershell Script Action to POST into ServiceNow staging table
- # ----------------------------------------------------------------------------------
-
- # Specify endpoint uri
- $uri = "https://yourinstancename.service-now.com/api/yourfolder/yourtablename"
-
- # Basic Authentication credentials
- $user = "yourservicenowusername"
- $pass = "yourservicenowpassword"
-
- # 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 HTTP method
- $method = "post"
-
- # ---- start description ----
- $descriptionString = @"
- %Device.DisplayName is %Device.State.
-
- Monitors down include:
- %Device.ActiveMonitorDownNames
-
- Brand:
- %Device.Attribute.Brand
- %Device.Attribute.Vendor
-
- Model:
- %Device.Attribute.Model
-
- Device Description:
- %Device.Attribute.Description
- "@
- # ---- end decription ----
-
- $deviceModel = @"
- %Device.Attribute.Model
- "@
-
- $deviceMAC = @"
- %Device.Attribute.MACAddress
- "@
-
- $JSON = ConvertTo-Json @{
- alert_severity = "Exception"
- short_description = "EXCEPTION: Monitors down for %Device.DisplayName"
- description = $descriptionString
- ip_address = "%Device.Address"
- mac_address = $deviceMAC
- device_name = "%Device.DisplayName"
- device_type = "%Device.Role"
- device_model = $deviceModel
- service_owner = "Network & Data Centre Services"
- device_link = "https://yourwugserver:443/NmConsole/#v=Wug_view_dashboard_DeviceStatus/p=%7B%22deviceId%22%3A%Device.DatabaseID%7D"
- }
-
- $Response = Invoke-WebRequest -Uri $uri -Headers $headers -Method $method -Body $JSON -UseBasicParsing
-
- # Use available context variables as needed
- $resultjson = convertfrom-json $Response.Content
- $resultText = $resultjson.result.message
-
- if($Response.StatusCode -eq 200)
- {
- $actionResult = 0;
- $resultText = "ServiceNow " + $resultText
- }
- else
- {
- $actionResult = 1;
- $resultText = "Error- row not added: $( $resultText )"
- }
- # Set the result
-
- $Context.SetResult($actionResult, $resultText)
The posts received in the ServiceNow look like this:
And the post confirmation is displayed in the action log.