Ever want to grab weather data for free to use on your dashboard or application? Here’s a quick PowerShell script that can be ran regularly to grab the latest temperature for an area/zip code.
#$data = Invoke-WebRequest "http://api.openweathermap.org/data/2.5/forecast/city?zip=65803&APPID=108119229b0bf3ad9faef37afb1fe855&units=Imperial&mode=xml"
$url = "http://w1.weather.gov/xml/current_obs/display.php?stid=KSGF" # Remote XML location
$saveFile = "\\Server\Shares\Scripts\Weather\weather_output.xml" # Local XML download location
Invoke-WebRequest -Uri $url -OutFile $saveFile # Download and save XML
$loadedXML = [xml]$loaded = Get-Content -Path $saveFile # Load XML
$temperature = $loadedXML.current_observation.temp_f # Read temp_f line in XML
If you want to push the temperature data elsewhere, say to InfluxDB to display with something like Grafana you can send it with Curl. You’d simply add additional code like at the bottom of your script –
cmd /c \\Server\Shares\Scripts\influx\curl.exe -i -XPOST http://SERVER.tld.local:8086/write?db=grafana_test --data-binary "wx_temp,host=""INFLUXSERVER"" value=$temperature"
It’s worth mentioning that this specific example uses NOAA or OpenWeatherMap, both have limitations on the amount of times you can query data in 24-hours unless you subscribe, so keep that in mind. Typically, weather data doesn’t change much or update that much, so I found 1 request per hour was typically enough and kept my dashboards fairly accurate.