Forum Discussion

Idan's avatar
8 years ago

Webservice monitor

Hi,

Below is a Powershell script to monitor a WebService that need NTLM authentication,

The script will return the status code and has an option to search a text in the results (e.g. of the response is a web page or XML) and check if a string exists.

I created 3 data points:

FoundString

ResponseTime (responseTime    None. Use the value directly)

StatusCode

 

You can create an alert if the status code is !=200 or the ResponseTime is more than X ms.

Hope you will find it useful

$url="http://##system.sysname##/GetAllEquimpentTypes"
$username = "##wmi.user##"
$password =  "##wmi.pass##"
$strintToFind = "ProbersTypes"


$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password)
$webpage = $webclient.DownloadString($url)
$WebOutput =  $webclient.DownloadString($url)

$req = [system.Net.WebRequest]::Create($url)
# method 1 $req.UseDefaultCredentials = $true
$req.Credentials = new-object System.Net.NetworkCredential($username, $password); 

try
{
    $res = $req.GetResponse()
}
catch [System.Net.WebException]
{
    $res = $_.Exception.Response
}

$int = [int]$res.StatusCode
$status = $res.StatusCode


if ($WebOutput.Contains($strintToFind)) 

    write-host "FoundString= 1"
}

else
{
    write-host "FoundString= 2"
}


write-host "StatusCode="$int
write-host "StatusDetails="$status