Friday Fun: Job Announcer

announcer-blueLast week I came across some people having fun with the SAPI.SPVoice COM object. This is the object that lets your computer “speak”. If you’ve never played with this, it is very simple to do in PowerShell. The voice quality in Windows 7 and 8 is quite nice, as far as synthetic voices go.

$v = new-object -ComObject "SAPI.SPVoice"
$v.speak("Powershell rocks!")

This got me to wondering…what about using this as a job announcer? Instead of having to go back periodically to check the status of a background job, why not have the job notify me when it is finished? All I would need to do is add a few lines of code to the scriptblock to create the voice object and announce when the job is complete. Here’s a sample.

#define a scriptblock for the job
$sb = {
 #find all errors and warning in the system eventlog since midnight yesterday
 Get-Eventlog -LogName System EntryType Error,Warning After (Get-Date).AddHours(24).Date
 #notify that the job has finished
 (New-Object -ComObject SAPI.SPVoice).Speak("Thank you for your patience. Your job $jobname has finished.") | Out-Null
#define a name for the job that can be passed to the scriptblock
$jobname = "Get Latest Eventlogs"
Startjob -Name $jobname ScriptBlock $sb -ArgumentList $jobname

The scriptblock could simply announce that the job is finished. If I only have a single job, that could be fine. But I wanted the scriptblock to be “aware” of what job it was running so I could have a more meaningful announcement. Thus, I pass the job name to the scriptblock so the voice object can use it in the announcement. It probably wouldn’t be too difficult to build a wrapper function to incorporate the voice announcement with the scriptblock. But I’ll leave that for you. If you come up with something, I hope you’ll share it and let me know.

$v.speak("Enjoy and Happy New Year")

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati