Fun With Alerts (“`a”)

Fun With Alerts (“`a”)

  • Comments 11

I just decided to restart Outlook to see if that made some weirdness I was experiencing go away.  As you may or may not know, the Outlook process often is running for quite a bit after the Outlook window goes away.  As such, you really don’t want to start another instance of Outlook until the first version is done. 

In V2, we now have the WAIT-PROCESS command so what I wanted to do was:

PS> Wait-Process Outlook; Outlook

The problem with that is that Outlook is not in the Path so it doesn’t work.  I could go figure out how to do this but I didn’t want to spend the time right now so instead I did the following:

PS> Wait-Process Outlook; “`a”

What that does is wait for the process to end and then emit an “Alert” (same as BEEP).  I waited for the alert and then knew it was safe to restart Outlook.  Anyway – I don’t know how many people know about Alert so I thought I would blog it. 

If you read the help on About_Quoting_Rules, you see a list of  the following special characters are recognized by Windows PowerShell:

    `0    Null
    `a    Alert
    `b    Backspace
    `f    Form feed
    `n    New line
    `r    Carriage return
    `t    Horizontal tab
    `v    Vertical tab

 

Experiment!  Enjoy!  Engage!

Jeffrey Snover [MSFT]
Distinguished Engineer
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post
  • PingBack from http://microsoft-sharepoint.simplynetdev.com/fun-with-alerts-%e2%80%9ca%e2%80%9d/

  • Say, this doesn't work with PowerShell v1 on Vista SP1. Is this a v1 thing or an SP1 thing?

  • @Nathan,

    The blog post mentions v2, which is PowerShell v2.  V2 is currently only available as a CTP release (currently CTP3).

  • @Nathan, Read carefully, v2 only (for Wait-Process).

    @Jeffery, I use alert in tab expansion.  It seems that if the TabExpansion function returns nothing, PowerShell does some basic tab completion anyway.  So I have to return something if I want no changes.  Returning any of the other special characters causes output buffer weirdness.  Except alert!  It is "something", yet it does not cause a change in the output buffer, just what I want (even if it is slightly annoying).

  • I have Vista SP1 and CTP3 and no beep for me.

  • @David,

    An easier test would simply be:

    PS>write-host "`a`"

    If that doesn't work, there's something up with your system config.

  • @Marco @David

    write-host "`a`"

    There is an additional escape ` in the end. It should be write-host "`a"

  • Fun with Wait-Prosess EXAMPLE 1 from help

    EXAMPLE 1

    $nid = (get-process notepad).id

    stop-process -id $nid

    wait-process -id $nid

    I run the example and get this:

    Get-Process : Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is

    set to Stop: Cannot find a process with the name 'notepad'. Verify the process name and call the cmdlet again.

    At C:\ a1.ps1:1 char:20

    + $nid = (get-process <<<<  notepad).id

       + CategoryInfo          : OperationStopped: (:) [Get-Process], ParentContainsErrorRecordException

       + FullyQualifiedErrorId : ActionPreferenceStop,Microsoft.PowerShell.Commands.GetProcessCommand

    Right, I forgot to start Notepad. This demonstrates and issue though.

    Command Wait-Process by default is not convenient for the task “ensure something is not running” – if it is not running then your script may fail.

    I start Notepad, run the example again and get this:

    Wait-Process : Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is

    set to Stop: Cannot find a process with the process identifier 5772.

    At C:\a1.ps1:3 char:13

    + wait-process <<<<  -id $nid

       + CategoryInfo          : OperationStopped: (:) [Wait-Process], ParentContainsErrorRecordException

       + FullyQualifiedErrorId : ActionPreferenceStop,Microsoft.PowerShell.Commands.WaitProcessCommand

    This looks like a buggy example (in fact I did not even have to run this to see a bug there).

    The proposal: do test all examples from help, quite a number of them simply do not work, some others work only sometimes (may depend on the current provider, for example).

    As for the "`a", it did not work for me on available for the test Vista machine.

  • I think you could have used 'Invooke-Item Outlook' because its in the App Paths registry key.

  • On Vista x64, the Beep service is not supported, so "`a" won't do anything. `a does work on Win7 x64.

  • Just wanted to clarify something: I am well aware that this post is on the topic of v2, and I'm sorry for getting off-topic. However, "`a" is not a new v2 feature; it was included in v1 RTW, and my point is that, as Louis said, it doesn't work on Vista -- it's a regression. Although unlike Louis, I have 32-bit Vista (SP1).

    Presumably Louis has figured out the problem shared between 64-bit Vista and 32-bit Vista SP1: Beep doesn't work; in fact, testing later, even [Console]::Beep() (in PS) or Console.Beep() (in VB.NET or C#) doesn't work.

Page 1 of 1 (11 items)