MSPSS: is there life after the helpdesk?

sharing solutions to uncommon IT problems

Powershell: Web Service Monitoring

leave a comment »


<#
Script: WebServiceInvokeTest
Synopsis: This script can consume any webservice and logs a message in the event log in case of success or failure.
The script can be amended in the if block to also return values to monitoring services (I use it for SCOM but it should work for other monitoring serivces as well.
If you want to integrate it with SCOM all you have to do is instantiate the bag before the if block and fill it with the result of the test (and return the bag at the end).
Parameters:
Server: Target server
URI: Path to the webservice
Method: The method you want to consume
Location: the XML path to the variable we check to see if the webservice is responsive
Success: The value of the variable that identifies a successful connection

Example: .\WebServiceInvokeTest.ps1 -server “YourServer-WebServiceHost” -URI “/WSPath/WSName.asmx” -method “WSMethod2Test” -location “FirstNode.SecondNode” -success “ReturnedValue4Success (i.e.: True)”

Author: Roberto Santoro
Date: 18/07/2014
#>
param (
[string]$server = $(throw “-server is required.”),
[string]$URI = $(throw “-URI is required.”),
[string]$method = $(throw “-method is required.”),
[string]$location = $(throw “-location is required.”),
[string]$success = $(throw “-success is required.”)
)

New-EventLog –LogName Application –Source “MyScript” -ErrorAction SilentlyContinue

$URL = “http://” + $server + $URI

try {
$Proxy = New-WebServiceProxy -uri $URL -namespace WebServiceProxy -UseDefaultCredential

[xml]$xml1 = $Proxy.”$method”.invoke()

$result = Invoke-Expression “`$xml1.$location”

if($result -eq $success){
Write-EventLog –LogName Application –Source “MyScript” –EntryType Information –EventID 100 –Message “The webservice test on $URL was successful.`r`nThe XML returned was: $($xml1.InnerXml)”
}
else {
Write-EventLog –LogName Application –Source “MyScript” –EntryType Error –EventID 101 –Message “The webservice test on $URL returned an error. The XML returned was: $($xml1.InnerXml)”
}
}
catch
{
Write-EventLog –LogName Application –Source “MyScript” –EntryType Error –EventID 101 –Message “The webservice test on $URL returned an error. Exception Message: $($_.Exception.Message)”
}

Advertisements

Written by zantoro

July 23, 2014 at 11:39 am

Posted in Powershell

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: