Consuming weather WebService with PowerShell

Update: Added the code in text form, after the jump.

Man, you’ve gotta love PowerShell.

Although I’m mostly into GUI and user experience, I just love console applications.

They’re super fast, powerful, concise, and if written correctly they are also easy to use (there’s that user experience theme again).

If I had to sum up the benefits of PowerShell in one sentence it would be this:

“Object oriented, interpreted, .net enabled , well structured shell and scripting environment.”

To install Windows PowerShell, just go here.

Now have a look at this cool little script I wrote last night.

It uses Yahoo! weather web service to get the local weather (in this example, local means Israel’s international airport, which happens to be close to where I live).

Here’s a snapshot of the script inside PowerGui so you can see all the nice color coding, the actual text is after the jump.

script source code

Click on the image above to open it full-size and let’s go over the lines real quick.

Line 2: creates a new .NET WebClient object.

Line 3: calls Yahoo’s WebService by downloading the result xml, converting it to a .NET XmlDocument object on the fly.

Lines 5,6,12,13,21-25: get a string value out of an xml node or attribute.

Some other interesting things to consider:

  • Lines 8,14 and 26 use the -f formatter, which works basically like C#’s String.Format().
  • Also note the pipeline (|) used to manipulate the text color in these lines.
  • To get the weather someplace else, all you need to do is modify the url, see their instruction page for more info.

And this is how it looks when I run it:

function weather {
$a = New-Object net.webclient
[
xml]$x = $a.DownloadString(http://weather.yahooapis.com/forecastrss?p=ISXX0011&u=c)

$loc = $x.GetElementsByTagName(yweather:location).item(0)
$current = $x.GetElementsByTagName(yweather:condition).item(0)
“”
Location: {0} -f $loc.getattribute(city) | Write-Host -ForegroundColor gray
“”
Current weather $([datetime]::Now)
———————————–
$text = $current.getattribute(text)
$temp = $current.getattribute(temp)
`t{0}, {1}°C -f $text, $temp | Write-Host -ForegroundColor yellow

$forecasts = $x.GetElementsByTagName(yweather:forecast)
“”
Forecasts
———
foreach ($f in $forecasts) {
$day = $f.getattribute(day)
$date = $f.getattribute(date)
$text = $f.getattribute(text)
$low = $f.getattribute(low)
$high = $f.getattribute(high)
`t{0}, {1}: {2}, {3}-{4}°C -f $day, $date, $text, $low, $high | Write-Host -ForegroundColor green
}
“”

}

Published in: on April 28, 2008 at 14:43 Comments (3)

The URI to TrackBack this entry is: http://fortheloveofcode.wordpress.com/2008/04/28/powershell-webservice-client/trackback/

RSS feed for comments on this post.

3 Comments Leave a comment.

  1. i’d be interested in getting the code cheers dude. Save me typing it out :)

    Matt

  2. I’ve added the code to the post.
    Cheers.

  3. Writing this as an object let’s you do some other cool things with it (blog.sapien.com/index.php/2007/03/21/powershell-temperature/) although I do like the color coding. You can also have fun like this: blog.sapien.com/index.php/2006/12/07/weather-report/


Leave a Comment