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
The 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.
Darn cool stuff Jeff – don’t let the PowerShell Police catch you using a GUI.
I am a neophyte with PowerShell. I tried your code and immediately got an error saying (Conditions) was not a cmdlet ….
I tried several work-arounds but could never get the -tooltip to show the value of Conditions. What am I not understanding?
Thanks
If you run Get-Temperature.ps1 by itself you should get a small table with Temperature and Conditions. If that doesn’t work then nothing else will. I then put the second block of code into another script and call that script. You’ll find it easiest to hover your mouse over the bottom of the gauge to see conditions, otherwise it tends to pop up behind the graphic.
If you need to work on this further, please post in the PowerShell forum at ScriptingAnswers.com.
Thanks for this! I just thought about doing a script and decided to search and see if someone else had done it before and came across this page. I don’t use the GUI tools, but what I did was assign temperature and condition to Global variables and called the function in my profile and the last line of my profile that runs when I start Posh is:
“`nWelcome to Powershell, Jason. It is currently ” + $Temperature1 + ” degrees and ” + $Conditions1 + “.”
How fun!