I saw a posting last week in the forums HERE. The poster is looking to find out if they can plan sounds in a gadget when the data or value changes.
One thing to remember when dealing with gadgets, and their refresh option, is that you’re passing a PowerShell script to the gadget and it is that script that is being re-invoked each time.
I’ve provided previous examples of creating a stock ticker and flashing display. With both of these examples, it was the PowerShell script that did all of the magic in passing along the text to display to the gadget.
So here’s my example of adding voice when the data for a gadget changes:
1: #requires -version 2.0
2:
3: $hostname=hostname
4: $voice=new-object -com sapi.spvoice
5:
6: if($global:idleValue -eq $null){
7: $global:idleValue=get-counter "\\$hostname\Processor(_total)\% Idle Time"| `
8: select -exp countersamples|select -exp cookedvalue|foreach{[math]::round($_,0)}
9: $global:usedValue=100-$global:idleValue
10: [void]$voice.speak("The CPU is at $global:usedValue")
11: break
12: }
13:
14: $global:curIdleValue=get-counter "\\$hostname\Processor(_total)\% Idle Time"| `
15: select -exp countersamples|select -exp cookedvalue|foreach{[math]::round($_,0)}
16: $global:curUsedValue=100-$global:curIdleValue
17:
18: if($global:curUsedValue -lt 1){
19: $global:curUsedValue=0
20: }
21:
22: if($global:curUsedValue -gt $global:usedValue){
23: $diff=$global:curUsedValue-$global:usedValue
24: [void]$voice.speak("The CPU % util is up $diff to $global:curUsedValue")
25: }
26: elseif($global:curUsedValue -lt $global:usedValue){
27: $diff=$global:usedValue-$global:curUsedValue
28: [void]$voice.speak("The CPU % util is down $diff to $global:curUsedValue")
29: }
30: else{
31: [void]$voice.speak("The CPU is the same at $global:curUsedValue")
32: }
33:
34: $global:usedValue=$global:curUsedValue
35:
36: $global:curUsedValue
The above requires PowerShell v2 so that you have the new Get-Counter cmdlet.
I use a built-in feature in Windows to do the voice. The above code gets an initial CPU % utilization value, then uses that for subsequent iterations. Each time a new value for the current CPU % utilization is retrieved, and compared to the previous value. Regardless of whether the value goes up, down or stays the same, the voice will indicate the current status, and if the value changed, it will also say the delta value.
I wanted to show an example of using the special character “`a”, but it doesn’t work on my current computer. “`a” is a special character supported by PowerShell that should cause the computer to “beep”. You may want to try it simply like this:
If it works for you, something you may want to do with the code above is to have one “beep” when the value goes down and two “beeps” when the value goes up, for example.
So simply called something like this:
1: PS>./powergadgets.ps1|out-gauge -type digital -refresh 0:0:10
You may notice that when the gadget starts up, you should see a data connection error image show in the lower left-most corner of the gadget, but it will quickly go away and shouldn’t return again.
Go PowerGadgets!