OpsMgr: Probing a Web Service with with PowerShell 2.0 in a Two-State Monitor

PowerShell 2.0 has this interesting cmdlet New-WebServiceProxy that makes probing web services very easy. It seemed to me this would be a great way to probe web services with Opsmgr 2007 using the R2 PowerShell modules in OpsMgr 2007, which has been documented in an excellent blog series by my friend and colleague Stefan Koell, as well as in a great screencast by Brian Wren of Microsoft (both are excellent job aids).  I was working this example out whilst on a phone call the other, building on more simple examples available on the net. I am going to post here samples I used to probe a global weather web service hosted on a US government web site that hosts many useful web services. Here I have posted several tips for probing the web service, as well as a sample two-state monitor script you can use in OpsMgr 2007 R2 using the tutorials as I mentioned here (links to these as well below).

The script probes the web service for the temperature in a given city, and then provides us state and simple output based on weather the temperature is above or below a given threshold. However, we have to do some work to isolate the temperature from the output. There are many ways to do this no doubt (some more efficient than others), but  I’ll show you one way to parse the output to extract the value we’re looking for. (If you have thoughts on the subject, please feel free to post your thoughts in comments on this post)

Connecting to a Web Service
You can connect to the web service as shown here. By assigning to a variable as shown here, we basically have a “connection object” of sorts, we can use in our script to easily make calls to the web service

Finding the Web Service Methods
I needed to find the methods we could use to probe the web service. This is easily done with the get-member cmdlet. (Not sure what a ‘method’ is? See the explanation HERE).

The output reveals a “GetCitiesByCountry” method, which we can use  to retrieve cities available to us, as well as the “GetWeather”. Let’s have a quick look at how we can probe the service and  get

 image

Finding Available Cities
Here, we probe the web service with the GetCitiesbyCountry method, passing ‘United States’ as the country for which I’d like a list.

but the output was ugly…xml basically (snippet below).

image

I wanted to get a human readable list, so I used the .GetElementsByTagName demonstrated by PoSh architect Jeffrey Snover and many others (BING search on ‘PowerShell AND GetElementsByTagName’ for more details)

 
The output was a bit prettier this way
image
 

Retrieving the Weather
To retrieve the weather, we pass the name of the  city to the GetWeather method. As you’ll see in the output, we have some filtering to do to get down to just the temperature. You’ll notice the [] brackets, before variable names, which is basically used to declare the data type for the variable.

 
and the output of this
 
image
 
Filtering Down to the Temperature
The challenge in getting to the temperature is two-fold. First, we need to retrieve the contents of the <temperature> tag, then we need to isolate the temp and type that as a numeric value rather than a string. Here’s how you can retrieve just the <Temperature> from the GetWeather method
which gives us something like this – “73.9 F (23.3 C)”

and then trim this down to just a number using the .Split method to grab just the number to the left of the “F” in the string
 

 
and then assign to a variable, treating this as a numeric value for later comparison in an IF-THEN statement
 

And then with our IF-THEN, we can simply compare the value to our threshold and assign to a PropertyBag (using the OpsMgr scripting runtime). I have shown both options for submitting a  property bag. the first works great for testing on your computer, the second is what will be required when you add the script to OpsMgr.

Hopefully this gives you some ideas on how you might probe web services in your OpsMgr monitoring scripts. Sample script and links to tutorials for setting it up below.

Sample Script
This sample script probes the web service and isolates the tempurature. If the temp is above 70F (about 21C), it returns “Toasty” and adds a value (“WARM”) to a property bag…If below, it returns “Brrrr” and add a value (“COLD”) to the property bag. I live 10 minutes from the gulf of Mexico, so this represents I weather preference pretty accurately. 🙂


































































Setting Up A Two-State Monitor in OpsMgr

Here are the promised tutorials for setting up this script in OpsMgr. Thanks to Stefan and Brian for the great examples!

Series: How to create two-state monitors in OpsMgr with R2 PowerShell modules (by  Stefan Koell, MVP)

Video (by Brian Wren, MS) – http://technet.microsoft.com/en-us/ff723797.aspx

More PowerShell Samples for System Center

You can find all sorts of PowerShell scripts for System Center products at PowerShell Scripts for System Center (Master Collection) or in our Downloads section.

Follow System Center Central via Twitter and RSS

Twitter_icon rss_big_default_300x300

0 thoughts on “OpsMgr: Probing a Web Service with with PowerShell 2.0 in a Two-State Monitor

  1. Profile photo of Andreas ZuckerhutAndreas Zuckerhut

    Weird… I recently needed to create a monitor using that but ended up calling an external script because the workflow terminated in between.

    Can’t remember which function made it terminate but I have the same problem when I use get-pssnapin without any options.

  2. Profile photo of Dottest37Dottest37

    Hello Mr Zerger
    Im trying to contact you via Private Message, but I dont know how to do it on this forum.

    Id like to ask you a question regarding my particular Web Services,,
    Thanks a lot!

Leave a Reply