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.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
#requires -version 3.0 Function Get-MyWeather { <# .Synopsis Get the weather for a US zip code .Description This command will query the Yahoo weather service and return weather conditions for a US zip code. .Example PS C:\> get-myweather 98133 Location : Seattle, WA Date : 2/16/2015 4:53:00 AM Temperature : 45 Low : 38 High : 60 Conditions : Mostly Cloudy .Notes Learn more about PowerShell:Essential PowerShell Learning Resources**************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** .Link Invoke-RestMethod #> Param( [Parameter(Position=0,HelpMessage="Enter a United States zip code")] [ValidateNotNullorEmpty()] [string]$Zip='00000' ) #construct the URI $uri = "http://xml.weather.yahoo.com/forecastrss?p=$Zip" Try { #gets XML data $data = Invoke-RestMethod -Uri $uri -ErrorAction Stop write-verbose ($data | out-string) if ($data.title -ne 'city not found') { #get title string and parse out location and time using #named regular expression captures $rx = "(?<location>\S+,\s\S+) at (?<time>\d{1,2}:\d{2}\s(a|p)m)" $data.title -match $rx | out-Null Write-Verbose ($matches | out-string) $location = $matches.location $dt = $matches.time -as [datetime] #write a custom object to the pipeline [pscustomobject][ordered]@{ Location = $location Date = $dt Temperature = $data.condition.temp -as [int] Low = $data.forecast[0].low -as [int] High = $data.forecast[0].high -as [int] Conditions = $data.condition.text } #close hash table } else { Write-Warning "No city found for zip code $zip" } } #Try Catch { #throw the exception Throw $_ } } #end function
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.
Hope you are staying warm.
Thanks for the fun code sample, Jeff. Sorry to hear that your freezing your wiggly parts off. I ashamed to say that we here in the Nor-Cal Posh area are suffering through days with highs in the mid 70’s, that’s right it was 75 here yesterday 2/15/15. Thanks Again for the fun posh sample and for god’s sake put a jacket on. Gotta go water skiing (joke). See Ya In Cyber Space, and keep the PShell open.
Fortunately I work at home so I don’t have to venture out.
Love the script Jeff, however you should be aware that the P= option has been depreciated by yahoo and will be phased out in the next release. I’m in Canada myself so obviously your script won’t work for me due to the difference in zip/postal codes so I’m trying to find a way to be able to query for the woeid, of course yahoo doesn’t exactly have the greatest documentation on how to find said beast for a given location without loading up the webpage and copying it from there.
Look at the functions I posted today on using the WOEID.
Well damn, didn’t see that 🙂 Thanks Jeff!