I trust by now you are realizing how valuable Windows PowerShell is as a management tool. With a one line command you can accomplish an extraordinary amount of work. Sometimes this work may be long running, which is where background jobs come in handy. Or you may simply kick off a long running script and go about your other administrative tasks. Unfortunately, you have to keep stopping what you’re doing to check and see if your script or job has finished. But there is a better way, assuming you are running Windows Vista or later.
When Don Jones was at SAPIEN Technologies, I believe he wrote a blog entry about creating a pop balloon notification in the system tray. This handy system could be used to notify the logged on user just of about anything. I took the liberty of revising the function and turned it into a PowerShell v2.0 function called Show-Balloon.
|
1 |
Function Show-Balloon { |
|
1 |
<span style="color: #008000">#requires -version 2.0</span> |
|
1 |
<<span style="color: #008000">#</span> |
|
1 |
.Synopsis |
|
1 |
Display a balloon tip message <span style="color: #0000ff">in</span> the system tray. |
|
1 |
.Description |
|
1 |
This <span style="color: #0000ff">function</span> displays a user-defined message as a balloon popup <span style="color: #0000ff">in</span> the system tray. This <span style="color: #0000ff">function</span> |
|
1 |
requires Windows Vista or later. |
|
1 |
.Parameter Message |
|
1 |
The message text you want to display. Recommended to keep it short and simple. |
|
1 |
.Parameter Title |
|
1 |
The title <span style="color: #0000ff">for</span> the message balloon. The <span style="color: #0000ff">default</span> is Attention: $env:username, where the variable |
|
1 |
will be replaced by the %USERNAME% environmental variable. |
|
1 |
.Parameter MessageType |
|
1 |
.The type of message. This value determines what type of icon to display. Valid values are |
|
1 |
Error, Warning, Info and None. The <span style="color: #0000ff">default</span> is Info. This paramater has an alias of <span style="color: #006080">'type'</span>. |
|
1 |
.Parameter Duration |
|
1 |
The number of seconds to display the balloon popup. The <span style="color: #0000ff">default</span> is 10. |
|
1 |
.Example |
|
1 |
PS C:\> Show-Balloon <span style="color: #006080">"Your script has finished."</span> -duration 15 -Title <span style="color: #006080">"Hey, You!!"</span> |
|
1 |
Displays a simple balloon popup <span style="color: #0000ff">for</span> 15 seconds. |
|
1 |
.Example |
|
1 |
PS C:\> Show-Balloon <span style="color: #006080">"There was an error with the script: $error[0].Exception.Message"</span> -messagetype <span style="color: #006080">"Error"</span> |
|
1 |
Display an error balloon tip showing the exception message from the last error PowerShell detected. |
|
1 |
.Example |
|
1 |
PS C:\> Invoke-command -computer $env:computername -scriptblock {. c:\scripts\show-balloon.ps1; get-eventlog -logname system -newest 500;show-Balloon <span style="color: #006080">"Background job complete."</span>} -asjob |
|
1 |
This example uses Invoke-Command to execute a potentially long running expression on the <span style="color: #0000ff">local</span> computer. When |
|
1 |
it is finished, a balloon message is displayed. Because the job runs <span style="color: #0000ff">in</span> a separate scope, the <span style="color: #0000ff">script</span> containing |
|
1 |
the Show-Balloon <span style="color: #0000ff">function</span> is dot sourced. After the Get-Eventlog cmdlet finishes, then the Show-Balloon <span style="color: #0000ff">function</span> |
|
1 |
is run, displaying the message. |
|
1 |
.Inputs |
|
1 |
None |
|
1 |
.Outputs |
|
1 |
None |
|
1 |
.Link |
|
1 |
http://jdhitsolutions.com/blog |
|
1 |
.Link |
|
1 |
Write-Warning |
|
1 |
Write-Error |
|
1 |
.Notes |
|
1 |
NAME: Show-Balloon |
|
1 |
VERSION: 1.0 |
|
1 |
AUTHOR: Jeffery Hicks |
|
1 |
LASTEDIT: 11/12/2009 |
|
1 |
<span style="color: #008000">#></span> |
|
1 |
[CmdletBinding()] |
|
1 |
Param ( |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Position=0, |
|
1 |
Mandatory=$True, |
|
1 |
HelpMessage=<span style="color: #006080">"The message text to display. Keep it short and simple."</span>)] |
|
1 |
[string]$message, |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"The message title"</span>)] |
|
1 |
[string]$Title=<span style="color: #006080">"Attention $env:username"</span>, |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"The message type: Info,Error,Warning,None"</span>)] |
|
1 |
[ValidateSet(<span style="color: #006080">"Info"</span>,<span style="color: #006080">"Error"</span>,<span style="color: #006080">"Warning"</span>,<span style="color: #006080">"None"</span>)] |
|
1 |
[Alias(<span style="color: #006080">"type"</span>)] |
|
1 |
[string]$MessageType=<span style="color: #006080">"Info"</span>, |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"The number of seconds to display the message."</span>)] |
|
1 |
[int]$duration=10 |
|
1 |
) |
|
1 |
Write-Verbose <span style="color: #006080">"Starting Function"</span> |
|
1 |
<span style="color: #0000ff">if</span> ($verbosePreference <span style="color: #cc6633">-eq</span> <span style="color: #006080">"Continue"</span>) { |
|
1 |
Write-Verbose <span style="color: #006080">"Getting Operating System"</span> |
|
1 |
Write-Verbose <span style="color: #006080">"OS: $((get-wmiobject win32_operatingsystem).caption)"</span> |
|
1 |
} |
|
1 |
Write-Verbose <span style="color: #006080">"Loading Assemblies"</span> |
|
1 |
<span style="color: #008000">#load Windows Forms and drawing assemblies</span> |
|
1 |
[reflection.assembly]::loadwithpartialname(<span style="color: #006080">"System.Windows.Forms"</span>) | Out-Null |
|
1 |
[reflection.assembly]::loadwithpartialname(<span style="color: #006080">"System.Drawing"</span>) | Out-Null |
|
1 |
<span style="color: #008000">#define an icon image pulled from PowerShell.exe</span> |
|
1 |
Write-Verbose <span style="color: #006080">"Extracting the icon from PowerShell.exe"</span> |
|
1 |
$icon=[system.drawing.icon]::ExtractAssociatedIcon((join-path $pshome powershell.exe)) |
|
1 |
Write-Verbose <span style="color: #006080">"Defining `$notify"</span> |
|
1 |
$notify = new-object system.windows.forms.notifyicon |
|
1 |
$notify.icon = $icon |
|
1 |
$notify.visible = $True |
|
1 |
Write-Verbose <span style="color: #006080">"Evaluating MessageType parameter $messagetype"</span> |
|
1 |
<span style="color: #008000">#define the tool tip icon based on the message type</span> |
|
1 |
<span style="color: #0000ff">switch</span> ($messagetype) { |
|
1 |
<span style="color: #006080">"Error"</span> { $messageIcon=[system.windows.forms.tooltipicon]::Error} |
|
1 |
<span style="color: #006080">"Info"</span> {$messageIcon=[system.windows.forms.tooltipicon]::Info} |
|
1 |
<span style="color: #006080">"Warning"</span> {$messageIcon=[system.windows.forms.tooltipicon]::Warning} |
|
1 |
Default {$messageIcon=[system.windows.forms.tooltipicon]::None} |
|
1 |
} |
|
1 |
Write-Verbose <span style="color: #006080">"Displaying message [ $Message ] for $duration seconds"</span> |
|
1 |
<span style="color: #008000">#display the balloon tipe</span> |
|
1 |
$notify.showballoontip($duration,$Title,$message,$MessageIcon) |
|
1 |
Write-Verbose <span style="color: #006080">"Ending function"</span> |
|
1 |
} <span style="color: #008000">#end function</span> |
|
1 |
Set-Alias sb Show-Balloon |
The function requires PowerShell version 2 and Windows Vista or later. Although it doesn’t check your operating system. I always like providing help information so the function can be used like a cmdlet.
The function displays a message in a popup balloon by default for 10 seconds. You can customize the duration, title and the icon. The icon is determined by the MessageType parameter and can be Error, Warning, Info or None.
The function is relatively simple using the Drawings and Forms .NET classes to create and display a NotifyIcon object.
You might use this function to display a message after a script has finished.
PS C:\> c:\scripts\myscript.ps1;Show-Balloon “Your script has finished.” -duration 15 -Title “Hey, You!!”
By using the ; to indicate a new line I can type a one line command and then move on to something else. You can easily incorporate variables into the message.
PS C:\> MyFunction ; Show-Balloon “There was an error with the script: $($error[0].Exception.Message)” -messagetype “Error”
Or use it with Invoke-Command.
PS C:\> Invoke-command -computer $env:computername -scriptblock {. c:\scripts\show-balloon.ps1;get-eventlog -logname system -newest 500;show-Balloon “Background job complete.”} -asjob
This example uses Invoke-Command to execute a potentially long running expression on the local computer. When it is finished, a balloon message is displayed. Because the job runs in a separate scope, the script containing the Show-Balloon function is dot sourced. After the Get-Eventlog cmdlet finishes, then the Show-Balloon function is run, displaying the message.
I hope you’ll let me know how you use this function.
Download Show-Balloon.ps1

Pingback: Tweets that mention The PowerShell Balloon Festival | The Lonely Administrator -- Topsy.com
Hey, you should check out Growl for stuff like this, it’s so much more flexible than systray balloons.
I’ve been meaning to try that out. The balloon trick is quick and dirty and doesn’t require anything.