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.

Enjoy.

Baby, It’s Cold Outside

I don’t know about your neck of the woods, but it is downright Arctic here. So I thought I’d polish up my PowerShell function to get weather data.

This function uses the Yahoo weather RSS feed for US locations. The function uses the Invoke-RestMethod to get an entry for a given zip code. I set my zip code as the default and suggest you do the same. You will need to modify the default value in the code above. Invoke-RestMethod gives me an XML document so it isn’t too difficult to pull out the values I want and construct a custom object. I even use some regular expression named captures to break out the location and time. So hopefully there are some good learning examples here.

Anyway, quite frigid here, and this is already 5 degrees warmer than when I got up.

weather

Hope you are staying warm.

Friday the 13th Fun

friday13 It is that time of year again. But instead of being freaked out by Friday the 13th, let’s have a little fun. Here is a collection of PowerShell one-liners all celebrating 13. And maybe you’ll even pick up something new about PowerShell.

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

%d bloggers like this: