Last 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.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
$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 = {
Param($jobname)
#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"
Start-job -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")
Nice! I actually did something similar for the same reason. I placed this little function into the script and it will reply with the users name that is running it.
Function Done()
{
$MyName = $env:username
$Tech=(Get-User $MyName).FirstName
$Voice = new-object -ComObject SAPI.SpVoice;
$Voice.Speak(“$Tech, Here are your results”) | Out-Null
}