DevOps Twitter Chat

DevOpsChat-tw_v1_sd This Thursday, February 26, I will be doing a live Twitter chat from 12PM to 1PM EST. The topic is DevOps but I’m sure we’ll talk about PowerShell and automation in general. Use hashtag #DevOpsChat to submit questions and follow along. You can follow me on Twitter as @JeffHicks. I hope you’ll join me.

A PowerShell Weather Service

I’ve been having some fun this week sharing a few tools for getting weather information and forecasts using PowerShell. To complete the story, today I have one more technique. Using PowerShell, you can query a web service for weather information. In simple terms a web service is like an application you can “run” via a web connection and sometimes even in a browser. The web service has “methods” you can invoke to do something. In the past if you wanted to interact with a web service in PowerShell you most likely needed to get down and dirty with the .NET Framework. Then PowerShell 3.0 brought us a new cmdlet, New-WebServiceProxy. With this cmdlet you can create an object that in essence becomes a wrapper to the web service. The web service methods become the object’s methods. Let me show you.

First, I’ll create my proxy object.

This object also has methods:

So how do I use the GetWeather method?

Seems straight forward. Let’s try. Wonder what it is like someplace warm?

Interesting. I can read it, but this sure looks like XML. If so, I should be able to retrieve the weather as an XML document.

Now I can explore it.

Wow. This seems simple. All I need is the CurrentWeather property and I have an easy to read result. I could probably run this as a one-liner.

Not necessarily easy to read but it works.

What about another city? Perhaps Charlotte, North Carolina, the site of the upcoming PowerShell Summit.

Well that worked, but that isn’t the city I had in mind. Going back to the web service methods, I remember seeing something to get cities by country. Let’s try that.

More XML. That’s find. I can use Select-XML to parse out the City node.

Now, expand each node.

Let’s try with the US now and find cities with Charlotte in the name.

It seems to me that the weather service data comes primarily from locations with airports. So if you live in a town without an airport, the best you can probably do is find a location near you with an airport. But now that I see what I need for Charlotte, North Carolina, I can revise my command.

Much better. But I don’t want to have to do that much typing for all of this so I put together a few functions to simplify the entire process. First, is a function to get a location.

The function does all the work of creating the web service proxy and retrieving cities by country. You can change the default to your country. I also added a parameter that will match on a city name.

I then wrote a function to make it easier to get weather information.

In looking at the XML, there were a few minor issues. One, the Wind node was repeated so I decided to combine the two into a single property. I also noticed that some of the node values had extra spaces. So I trimmed up the values as a I built an ordered hashtable which I then turned into a custom object. The end result is an easy to use tool.

You can even pipe between commands.

The object properties are all strings which means sorting or filtering is troublesome, but there is some good data here so I guess it is a trade-off.

Now you have several ways to check the weather, assuming you can’t simply look out a window. And hopefully you’ve learned a few new things about PowerShell along the way.

Getting the Weather Where On Earth

Yesterday I posted a popular article about using Invoke-WebRequest to get weather conditions. That function used the Yahoo web site but really only worked for US cities. So I also cleaned up and revised another set of advanced PowerShell functions (required PowerShell 3) that can retrieve weather information for probably any location on Earth. The first piece of information you need is your WOEID, or “Where On Earth ID”. Here is a function to do just that.

To use the function you specify some sort of search criteria such as a postal code or city name.

I saved my WOEID as a variable in my PowerShell profile. This function also uses Invoke-RestMethod. I included a parameter to write an XML document to the pipeline instead in case you want to modify the function and need some help discovering the data. The second function, Get-Weather, uses the WOIED to get the current weather conditions. It too uses Invoke-RestMethod to retrieve the data. The resulting XML document is then parsed out using Select-XML to build an output object.

You can also change the temperature units.

I wrote these as two separate functions, I suppose I could have nested Get-Woeid inside Get-Weather, although the better option is probably to build a module. I’ll leave that for you. The functions are designed to take advantage of pipeline binding so that you can pipe Get-Woeid to Get-Weather.

You can even get weather for multiple locations.

This weather source includes a lot of information so I created a parameter to control how much detail to display. What you see above is basic information. But there is ‘extended’.

Or you can see everything with a detail setting of ‘all’.

Notice that url at the end? As the cherry on top, you can open the weather forecast in a browser using the Online parameter.

There you have it. No matter where you are you can check the weather. Or look out a window.


Advice, solutions, tips and more for the lonely Windows administrator with too much to do and not enough time.

%d bloggers like this: