MSPSS: is there life after the helpdesk?

sharing solutions to uncommon IT problems

Archive for the ‘Powershell’ Category

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)”
}

Written by zantoro

July 23, 2014 at 11:39 am

Posted in Powershell

Tagged with

Powershell: write in a Windows event log

leave a comment »


Hello,

this is really quick post about a Powershell quibble I faced today.
Writing in an event log is very easy but if you want to have your own Source (and most of the times you want to do that) you have to create the source first.

New-EventLog –LogName Application –Source “My Script”
Write-EventLog –LogName Application –Source “My Script” –EntryType Information –EventID 100 –Message “A Message”

Unfortunately if you have already created the source, you’ll get an error when trying to create it again the second time: “New-EventLog : The “My Script” source is already registered on the “localhost” computer.”

Most sources on the internet suggest to check if the source exists by doing: if (!(Get-Eventlog -LogName Application -Source “My Script”)) {
This a bad idea because Powershell has to parse the entire event log to find out if the Source exists, resulting in a very loooong query.
Personally, given that the error is a not blocking one, I resolved to run the command New-EventLog anyway and use the -ErrorAction SilentlyContinue parameter to keep the error quiet (but you still get other errors).

New-EventLog –LogName Application –Source “My Script” -ErrorAction SilentlyContinue

 

HTH,

Roberto.

Written by zantoro

July 18, 2014 at 11:29 am

Posted in Powershell

Tagged with