Getting Weather Forecast from an Airfield near You

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

PowerShell can access web services and automatically retrieve information such as weather forecasts. New-WebServiceProxy does all the work for you, and you only need to submit the web service URL. Note however that New-WebServiceProxy requires direct Internet access and cannot work over proxy servers.

This piece of code returns the names of all airfield weather stations in the United States:

$weather = New-WebServiceProxy -Uri http://www.webservicex.com/globalweather.asmx?WSDL
$city = ([xml]$weather.GetCitiesByCountry('United States')).NewDataSet.Table | 
  Select-Object -ExpandProperty City
$city 
Claiborne Range, Airways Facilit
Payson
Custer, Custer County Airport
Andover, Aeroflex-Andover Airport
Nogales Automatic Meteorological Observing System
Elkhart / Elkhart-Morton County
Saint Johnsbury
Salmon
(...)

Next, you can specify one of the airfields to get current weather information:

$weather = New-WebServiceProxy -uri http://www.webservicex.com/globalweather.asmx?WSDL
$data = ([xml]$weather.GetWeather('Seattle, Seattle Boeing Field','United States')).CurrentWeather
$data 

The result will look something like this:

Location         : SEATTLE BOEING FIELD, WA, United States (KBFI) 47-33N 122-19W 4M
Time             : Jan 02, 2013 - 03:53 AM EST / 2013.01.02 0853 UTC
Wind             :  Calm:0
Visibility       :  10 mile(s):0
SkyConditions    :  clear
Temperature      :  30.9 F (-0.6 C)
DewPoint         :  28.0 F (-2.2 C)
RelativeHumidity :  88%
Pressure         :  30.35 in. Hg (1027 hPa)
PressureTendency :  0.03 inches (0.9 hPa) lower than three hours ago
Status           : Success

To get the current temperature, for example, you could access the appropriate object property:

PS> $data.Temperature
 30.9 F (-0.6 C)
PS> ($data.Temperature -split '[\(\)]')[1]
-0.6 C
PS> ($data.Temperature -split '\(')[0]
 30.9 F 

Twitter This Tip! ReTweet this Tip!


Posted Jan 23 2013, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.