15 Feb
2010

PowerShell – background task that speaks to me

Category:General PostTag: :

Today I was testing a long running task where my basic scenario was to execute at the PowerShell prompt.

C:\dev> LongRunningTask.exe > Test1.txt

C:\dev> LongRunningTask.exe > Test2.txt

C:\dev> LongRunningTask.exe > Test3.txt

Change some code and do it all over again.

C:\dev> LongRunningTask.exe > Test4.txt

C:\dev> LongRunningTask.exe > Test5.txt

C:\dev> LongRunningTask.exe > Test6.txt

Although seemingly simple and really not that tough. It became really inefficient because I didn’t like sitting there waiting for it to finish. I was off reading blogs, responding to email, investigating other coding issues and generally forgetting about the long running tasks in the background. On top of that, when I came back to it I couldn’t remember what the last run was (was that test 2 or 3 or 5?) without querying disk to see what it was.

Sine I had all this time to kill in between test runs, I wrote a quick PowerShell script that would fully automate test 1,2,3 and stop. This was great and the first time I ran it was much more efficient as I didn’t have to keep checking back to see when to start it up again.

However, I not only had time to kill, but now I had 3 times that time to kill. I decided to elaborate on the script. I searched the tubes for a way to play a sound when a task was done, so I could be notified as to it’s progress, and completion.

I stumbled upon this blog, which was great for showing me how to play sounds within PowerShell.

http://scriptolog.blogspot.com/2007/09/playing-sounds-in-powershell.html

I implemented the sounds and was off and running on another batch of testing, all tuned in to the sounds of progress.

Her we go again, more time to think about how to improve the process (that I’m not even trying to improve).

What if the script talked back to me? Some more googlefoo and I found this great example blog.

http://huddledmasses.org/powershell-speaks/

Basically using the built in Microsoft speech api’s you can get PowerShell to talk to you. This was just too much fun for the day.

At the end of the day the script below is basically what I was using. Don’t have too much fun, as you might not get any real work done…

The content of this blog isn’t necessarily top notch, but I just had to blog about it, as I was very impressed as to how easy it was to work with the speech api’s. Never knew it could be so easy to make a script verbally interactive.

$Voice = new-object -com SAPI.SpVoice

function speak([string] $msg)
{
    $Voice.Speak( $msg, 1 )
}

function execScriptWithNotify([string] $completionMessage, [scriptblock] $scriptToExecute)
{
    . $scriptToExecute
    speak $completionMessage
}

function execLongRunningTask([scriptblock] $scriptToExecute)
{
    . $scriptToExecute

    Write-Host "Long running task complete!"
    speak "All tasks are done!"
}


# Start the long running task
execLongRunningTask {

    #***************************************************************
    #***************** How many times to run task? ***************** 
    #***************************************************************
    $iterationCount = 3
    
    for ($i=1; $i -le $iterationCount; $i++)
    {
        execScriptWithNotify "Task $i Complete" {
        
            #***************************************************************
            #***************** Long Running Task Goes Here ***************** 
            #***************************************************************
            Write-Host "begin long running task # $i"
            [System.Threading.Thread]::Sleep(3000);
            Write-Host "end   long running task # $i"
            #***************************************************************
            #***************************************************************
            #***************************************************************
        }
    }

}