PowerShell Temperature

I thought this was going to be a fast and furious example, but it took a whole lot longer than I expected. The good news is that I’ve done all the work for you.  You’re most welcome.

I love weather and living in central New York we get all kinds. So I decided I needed a thermometer on my desktop. Using PowerGadget’s Out-Gauge cmdlet this is pretty easy to do.

#Get-Temperature.ps1
$url=”http://xml.weather.yahoo.com/forecastrss?p=ZIPCODE”
$webClient = New-Object System.Net.WebClient
[xml]$data=$webClient.DownloadString($Url)

#create new object to hold values
$obj = New-Object System.Object
Add-Member -inputobject $obj -membertype NoteProperty -Name Temperature -value $data.rss.channel.item.condition.temp
Add-Member -inputobject $obj -membertype NoteProperty -Name Conditions -value $data.rss.channel.item.condition.text

Write-Output $obj

thermometer.pngThe script downloads an XML file with weather information. You need to substitute your zip code in the URL. I’ve “objectified” the function by defining some properties. There’s a lot more information in the XML file you can play with as well. I picked two properties that I wanted to use in my gauge. Because the configuration for the gauge is more than I care to type more than once, I put this code in a separate script so all I have to do is run it:

s:\posh\Get-Temperature.ps1 | out-gauge -style 4 -MainIndicator_Style Filler07 -value Temperature -type vertical -floating -tooltip {$_.Conditions} -tooltipenabled True -scales_0_max 120 -scales_0_min -20 -size 50,150 -refresh 0:30:0

 Be sure to specify the complete path to the script file. As written, the temperature is updated every 30 minutes. If I hover my mouse over the gauge I’ll also get current conditions. Hover over the “mercury” and I’ll get the current temperature.

I had hoped to add the code as a function to my profile, but PowerGadgets doesn’t necessarily load the functions from your profile so the refresh didn’t work. They’re looking into this issue for a future release. I also could have created an actual gadget file with PowerGadget Creator.

[Note 3/22/2003]

I just edited the second block of code.  I had posted code that won’t work. You’ll get a message about Condition not recognized as a cmdlet, etc. For the ToolTip I have to explicitly say, use the Conditions property from the current object, ie, the one in the pipeline.

 

Technorati tags: , , ,

del.icio.us tags: , , ,