The Power Behind the Shell
This cute little demo of Windows Powershell provides a spoken weather forecast. I found this thread on the PowerShell team blog. You simply enter your US zip code or Weather.com city code (eg. UKXX0943) and the script trundles off to Yahoo weather, retrieves an RSS feed containing the weather forecast for your location, then speaks the current conditions and extended forecast for your location using your computer's sound system. Amazing! I'd love to see this done so efficiently in any other shell. There's a challenge for you, Vijay! This example demonstrates the power of working with .Net objects and COM components instead of text.
I modified the code slightly from the original at the Sapien Technologies blog to make the script work with ISA Server when the firewall client is installed – it does this by explicitly telling the WebClient object not to use a proxy server (the firewall client then handles proxy authentication correctly). I also changed the default units to Celsius for European tastes.
Here's the code:
# WeatherReport.ps1
# This script will get the current weather conditions and forecast for the specified
# US zip code or Weather.com location code (eg. UKXX0943)
# from Yahoo weather, and using the SAPI.SPvoice object, speak the information for you.
# [TPL] Original script from http://sapien.eponym.com/blog/_archives/2006/12/7/2549282.html
# [TPL] 2006-12-13 - work around ISA proxy authentication by requesting the web client not
# to use a proxy server (the IS Firewall Client then uses SecureNAT).
# - Changed default units to Celsius for european consumption.
Function Get-Weather($zip=$(Throw("You need to specify a zip code!")),$Unit=$(Throw("Specify F or C for the temperature scale!")))
{
[string]$urlbase="http://xml.weather.yahoo.com/forecastrss"
#validate temperature scale
if ($Unit -eq "c") {
[string]$scale="Celsius"
$Unit="c"}
else {
[string]$scale="Farenheit"
$Unit="f"}
[string]$url=$urlbase + "?p="+$zip+"&u="+$Unit
#create the text to speech object
$voice=New-Object -com "SAPI.SPVoice"
Write-Host Connecting to $url
#Create .NET Webclient object
$webclient=New-Object "System.Net.WebClient"
$webclient.UseDefaultCredentials=1 #[TPL]
$webclient.Proxy = $webclient.GetEmptyProxy #[TPL] Don't use a proxy (ISA will use SecureNAT)
[xml]$data=$webclient.DownloadString($url)
if ($data.rss.channel.item.Title -eq "City not found") {
Write-Host "Could not find a location for" $zip}
else {
Write-Host $data.rss.channel.item.Title
#Speak the current conditions
[void]$voice.speak("You asked about the weather.")
[void]$voice.Speak($data.rss.channel.item.Title)
[void]$voice.speak("The current condition is")
[void]$voice.speak($data.rss.channel.item.condition.text+"!")
[void]$voice.speak("The current temperature is" +$data.rss.channel.item.condition.temp+"
degrees "+$scale)
#get forecast
for ($i=0; $i -le ($data.rss.channel.item.forecast.length)-1;$i++) {
[void]$voice.speak("The forecast for "+`
$data.rss.channel.item.forecast[$i].date+`
"is, "+$data.rss.channel.item.forecast[$i].text+"with a low of"+`
$data.rss.channel.item.forecast[$i].low+" degrees "+$scale+`
", and a high of"+$data.rss.channel.item.forecast[$i].low+" degrees "+$scale)
}
}
}
$zip=Read-Host "Enter a zip code for your weather query"
Get-Weather $zip "c"
Use Notepad to create a new file and copy & paste the above code into it. Save the file as GetWeather.ps1 (take care that Notepad doesn't add a .txt extension) then in your PowerShell window, CD to the directory where the file is saved and type .\GetWeather.ps1