Chinh Do

Give The Power of Speech and Sound to Your PowerShell Scripts

16th January 2010

Give The Power of Speech and Sound to Your PowerShell Scripts

posted in PowerShell, Programming |

Do you ever have the problem where you start a long running script (such as running a code build), multi-task on something else on another monitor while waiting for the script to finish, and then totally forget about the script until half an hour later? Well, here’s a solution your problem: have your script give you holler at you when it’s done.

Hello sir! I am done running your command!

In my library script file, I have the following functions to play sound files and to speak any text:

function PlayMp3($path) {
    # Use the default player to play. Hide the window.
    $si = new-object System.Diagnostics.ProcessStartInfo
    $si.fileName = $path
    $si.windowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
    $process = New-Object System.Diagnostics.Process
    $process.startInfo=$si
    $process.start() 
}

function PlayWav($path) {
    $sound = new-Object System.Media.SoundPlayer;
    $sound.SoundLocation="$path";
    $sound.Play();
}

function Say($msg) {
    $Voice = new-object -com SAPI.SpVoice
    $Voice.Speak($msg, 1 )
}

If you like the text-to-speech feature but find Windows’ speech engine lacking, check out Ivona. It’s a commercial text-to-speech engine but you are allow to generate and download short speech files for free personal use. Now, my script can nicely interrupt me to tell me when it’s done. Other online text-to-speech engines: vozMe, SpokenText.

If Making Noise Is Not Your Thing

If making noise is not your thing, consider displaying a message in the Notification Area. Here’s the code (courtesy Microsoft TechNet):

Build complete!

function Get-ScriptName {
    $MyInvocation.ScriptName
} 

function DisplayNotificationInfo($msg, $title, $type) {
    # $type - "info" or "error"
    if ($type -eq $null) {$type = "info"}
    if ($title -eq $null) {$title = Get-ScriptName}

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 
    # Specify your own icon below
    $objNotifyIcon.Icon = "C:CdoScriptsFolder.ico"
    $objNotifyIcon.BalloonTipIcon = "Info"  
    $objNotifyIcon.BalloonTipTitle = $title
    $objNotifyIcon.BalloonTipText = $msg

    $objNotifyIcon.Visible = $True 
    $objNotifyIcon.ShowBalloonTip(10000)
}
This entry was posted on Saturday, January 16th, 2010 at 1:34 am and is filed under PowerShell, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

There are currently 3 responses to “Give The Power of Speech and Sound to Your PowerShell Scripts”

  1. 1 On April 20th, 2010, BRW said:

    If you are going to create the notify object, its also a good idea to remove it.

    I have some code below with an example.

    Using –> $objNotifyIcon.Dispose

    I wrote this code for PowerShell v1.0 and also some of my function names do not follow the correct verb-noun naming conventions.

    ########################
    function ShowNotify_Icon([string]$msg)
    {
    $objNotifyIcon.BalloonTipIcon = ‘Info’
    $objNotifyIcon.Icon = ‘D:\Create_Notify_Icon\graph.ico’
    $objNotifyIcon.BalloonTipTitle = ‘Processing …’
    $objNotifyIcon.BalloonTipText = $msg
    $objNotifyIcon.Visible = $True
    $objNotifyIcon.ShowBalloonTip(9000)
    Sleep(9)
    }#ShowNotify_Icon

    ########################
    function HideNotify_Icon([string]$msg)
    {
    $objNotifyIcon.BalloonTipText = $msg
    $objNotifyIcon.ShowBalloonTip(9000)
    Sleep(9)
    $objNotifyIcon.Visible = $false
    }#HideNotify_Icon

    ########################
    #Start Here : )
    [void][System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon

    ShowNotify_Icon(‘This operation is starting . . .’)

    HideNotify_Icon(‘This operation has completed . . .’)

    $objNotifyIcon.Dispose
    #main

  2. 2 On April 20th, 2010, Chinh Do said:

    Hi BRW:

    This is a very useful tip, thanks! If you don’t mind, I will update my article with your code later.

    Chinh

  3. 3 On April 20th, 2017, rom said:

    Eveгythinbg is vᥱry open with a precise description of the issues.
    It was really informative. Yoour site is еxtremely helрful.
    Thank you for sharing!

    Feel free tto surf to my web blog: rom

Leave a Comment

  • Calendar

  • January 2010
    M T W T F S S
    « Dec   Feb »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031