Tim Long

Recent Posts

Tags

News

  • Locations of visitors to this page
    View Tim Long's profile on LinkedIn
    Tim Long
    StackOverflow.com
    Serverfault.com
    ASCOM Answers

Community

Email Notifications

TiGra Networks

My Family

Photo Galleries

SBS Groupies

Archives

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

Share this post: | | |

Comments

Ying Jin said:

I try to run it, but get this error:

The term '.\GetWeather.ps1' is not recognized as a cmdlet, function, operable program, or script file. Verify the term

and try again.

At line:1 char:16

+ .\GetWeather.ps1 <<<<

Any idea?

# December 21, 2006 2:54 AM

Tim Long said:

The only thing I can think of is that if you saved the file from notepad, then a .txt extension may have been added (notepad does this by default). So you may actually have a file called GetWeather.ps1.txt. To make matters worse, Explorer hides file extensions by default, so it can be confusing.

My original blog post has an attachment - try downloading the attachment by right-clicking on it and selecting Save Target As...

# December 21, 2006 3:02 AM