dcrum-to-service-now-incident.ps1

Example of a Microsoft PowerShell script for DC RUM 12.4 to create ServiceNow incidents using REST.

 # Data Center Real User Monitoring to ServiceNow Incidents script (SN/REST/INCIDENT)
# Version: 1.0
# Author: Pawel Siwek

#Parameters to be included in script call
param (
    [string]$dcrum_message = "",
    [string]$dcrum_uri = "",
    [string]$sn_node = "",	    
    [string]$sn_resource = "",
				[string]$sn_uri = "",
    [string]$sn_user_id = "",
    [string]$sn_passwd = "",	
    [bool]$debug = 0
)
 
Try
{

# Writing log information in debug mode including parameters that were included in the call
if ($debug -eq 1)
{
 $time=Get-Date
 "$time New alert notification received " | Add-Content service-now.log
 "Script parameters " | Add-Content service-now.log    
 foreach ($key in $MyInvocation.BoundParameters.keys)
 {
    $value = (get-variable $key).Value 
    $keyheader = $key.ToString().ToUpper();
    if (![string]::IsNullOrEmpty($value))
    {
        "$keyheader : $value" |   Add-Content service-now.log
    }
 }
}

# Getting first line of DCRUM description to be reported as SN message
if ($dcrum_message.Length -gt 0)
{
    # if DCRUM message has multiple lines we take first line for ticket title
    if ($dcrum_message.IndexOf("`n") -gt 0)
    {            
        $sn_message = $dcrum_message.Substring(0,$dcrum_message.IndexOf("`n"))
    }
    # else we take whole message
    else
    {        
        $sn_message = $dcrum_message;
    }
}
    

# Preparing ServiceNow JSON structure for events
$SN_json_text = @"
{
	'category': 'Network',
	'impact': '3',
	'urgency': '3',
	'priority': '5',
	'short_description': '',
	'comments': '',
	'u_dynatrace_dcrum_details': '',
	'correlation_id' : ''
}
"@

#Populating JSON object with data from DCRUM
$dcrum_uri = $dcrum_uri -replace '([\w\s]+): ', ''

$SN_json_object = $SN_json_text | ConvertFrom-Json
$SN_json_object.comments = $dcrum_message;
$SN_json_object.short_description = $sn_message;
$SN_json_object.correlation_id =[guid]::NewGuid().ToString();
$SN_json_object.u_dynatrace_dcrum_details =$dcrum_uri;

#Converting back JSON object to text format
$SN_json_text = $SN_json_object | ConvertTo-Json

if ($debug -eq 1)
{
    "JSON" | Add-Content service-now.log
        $SN_json_text | Add-Content service-now.log 
}

# Preparing HTTP request header
$RestApi_WebRequest = [System.Net.WebRequest]::Create($sn_uri)    
[string]$authInfo = $sn_user_id + ":" + $sn_passwd
[string]$authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($sn_user_id + ':' + $sn_passwd))
$RestApi_webRequest.Headers.Add("AUTHORIZATION","Basic $authinfo")
$RestApi_WebRequest.ContentType = "application/json"
$RestApi_WebRequest.Accept      = "application/json"
$RestApi_WebRequest.Method      = "POST"
$buffer = [System.Text.Encoding]::UTF8.GetBytes($SN_json_text)

# Preparing HTTP request body
$RequestStream = $RestApi_WebRequest.GetRequestStream()
$RequestStream.Write($buffer, 0, $buffer.Length)
$RequestStream.Flush()
$RequestStream.Close()

# Sending HTTP request
$RestApi_Response = $RestApi_WebRequest.GetResponse()

# Parsing HTTP Response
$responseStream = $RestApi_Response.GetResponseStream()
$ResponseReader = [System.IO.StreamReader]($responseStream)
$RestApi_ResponseBody =  $ResponseReader.ReadToEnd()
$responseStream.Close()     

# Writing to log
if ($debug -eq 1)
{
    $time=Get-Date
        "$time completed
        " | Add-Content service-now.log
}
    
}
# In case of error writing exception details to log file
Catch
{
    if ($debug -eq 1)
    {
           $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
           $time=Get-Date
           "$time error
           $ErrorMessage
           $FailedItem" | Add-Content service-now.log
    }
}