dcrum-to-service-now-incident-soap.ps1

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

  param (
    [string]$message = "",
	   [string]$application = "",
	   [string]$transaction = "",
	   [string]$reporting_group = "",
	   [string]$software_service = "",
	   [string]$server_ip = "",
	   [string]$server_dns = "",
	   [string]$operation = "",
	   [string]$task = "",
	   [string]$module = "",
	   [string]$service = "",
	   [string]$step = "",
	   [string]$site = "",
	   [string]$area = "",
	   [string]$region = "",
	   [string]$wan_link = "",
	   [string]$metric_val = "",
	   [string]$unit_of_measure = "",
	   [string]$threshold = "",
	   [string]$aux_metric_value = "",
	   [string]$aux_unit_of_measure = "",
	   [string]$alter_triggering_mode = "",
	   [string]$service_now_uri = "",
    [string]$service_now_user_name = "",
    [string]$service_now_password = "",
    [string]$dcrum_url = "",
    [bool]$debug = 0
)

Function Soap_Request_Basicauth()
{
Param (
            [Parameter(Mandatory=$True,ValueFromPipeline=$False,ValueFromPipelinebyPropertyName=$False)]
            [xml]$SOAP_Request    
   )
    Begin {  }
    Process {       
    $SOAPRequest = $SOAP_Request
    Write-host "Sending SOAP Request To Server: $SN_uri"    
    $Soap_WebRequest = [System.Net.WebRequest]::Create($SN_uri)    
    [string]$authInfo = $SN_username + ":" + $SN_password
    [string]$authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($SN_username + ':' + $SN_password))
    $Soap_webRequest.Headers.Add("AUTHORIZATION","Basic $authinfo")
    $Soap_WebRequest.Headers.Add("SOAPAction",$SN_method)
    $Soap_WebRequest.ContentType = "text/xml;charset=`"UTF-8`""
    $Soap_WebRequest.Accept      = "text/xml"
    $Soap_WebRequest.Method      = "POST"
    Write-host "Initiating Send."    
    $RequestStream = $soap_WebRequest.GetRequestStream()
    $SOAPRequest.Save($requestStream)
    $requestStream.Close()
    Write-host "Soap Request sent, Waiting For Response."       
     $resp = $soap_WebRequest.GetResponse()
     $responseStream = $resp.GetResponseStream()
     $soapReader = [System.IO.StreamReader]($responseStream)
     $ReturnXml = [xml] $soapReader.ReadToEnd()
     $responseStream.Close()     
     Write-host "Response Received."
     }
     End {  write-output $Returnxml }
}

Try
{
    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
	        }
        }
    }

	$SN_uri = $service_now_uri
	$SN_username = $service_now_user_name
	$SN_password = $service_now_password 
	$SN_method = 'http://www.service-now.com/imp_notification/insert' 
	$SN_uuid = [guid]::NewGuid().ToString() 
	$SN_timestamp = Get-Date -format s
	$SN_message=""

	if ($message.Length -gt 0)
	{
        # if DCRUM message has multiple lines we take first line for ticket title
        if ($message.IndexOf("`n")>0)
        {
        	$SN_message = $message.Substring(0,$message.IndexOf("`n"))
        }
        # else we take whole message
        else
        {
            $SN_message = $message;
        }
	}
    
    $SN_dcrum_url = ""
    if ($dcrum_url.Length -gt 0)
    {
        $http_pos = $dcrum_url.IndexOf("http")
        if ($http_pos -gt 0)
        {
            $SN_dcrum_url = $dcrum_url.Substring($http_pos, $dcrum_url.Length - $http_pos)
        }
        else
        {
            $SN_dcrum_url = $dcrum_url
        }
    }

	$SN_xml = [xml]@"
<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:imp="http://www.service-now.com/imp_notification">
	<soapenv:Header/>
	<soapenv:Body>
		<imp:insert>
			<category>Network</category>
			<comments></comments>
			<message></message>
			<severity>1</severity>
			<source>DCRUM</source>
			<state>New</state>
			<timestamp></timestamp>
			<uuid></uuid>
		</imp:insert>
	</soapenv:Body>
</soapenv:Envelope>
"@

	$SN_xml.Envelope.Body.insert.timestamp = $SN_timestamp.ToString()
	$SN_xml.Envelope.Body.insert.uuid = $SN_uuid
	$SN_xml.Envelope.Body.insert.comments = $message
	$SN_xml.Envelope.Body.insert.message = $SN_message

    if ($debug -eq 1)
    {
        "SOAP Envelope" | Add-Content service-now.log
	    $SN_xml.InnerXml | Add-Content service-now.log 
    }

    # Sending ticket to service
	$response = (Soap_Request_Basicauth -SOAP_Request ($SN_xml.InnerXml))
	
    if ($debug -eq 1)
    {
        $time=Get-Date
	    "$time completed
	    " | Add-Content service-now.log
    }
}
Catch
{
    if ($debug -eq 1)
    {
	    $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
	    $time=Get-Date
	    "$time error
	    $ErrorMessage
	    $FailedItem" | Add-Content service-now.log
    }
}