#Requires -version 2.0 # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # "Those who forget to script are doomed to repeat their work." # This requires Windows Vista or later. # DISCLAIMER AND WARNING: # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY # KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. # TEST THOROUGHLY IN A NON-PRODUCTION ENVIRONMENT. IF YOU DON'T KNOW WHAT THIS # SCRIPT WILL DO...DO NOT RUN IT! Function Show-Balloon { #requires -version 2.0 <# .Synopsis Display a balloon tip message in the system tray. .Description This function displays a user-defined message as a balloon popup in the system tray. This function requires Windows Vista or later. .Parameter Message The message text you want to display. Recommended to keep it short and simple. .Parameter Title The title for the message balloon. The default is Attention: $env:username, where the variable will be replaced by the %USERNAME% environmental variable. .Parameter MessageType .The type of message. This value determines what type of icon to display. Valid values are Error, Warning, Info and None. The default is Info. This paramater has an alias of 'type'. .Parameter Duration The number of seconds to display the balloon popup. The default is 10. .Example PS C:\> c:\scripts\myscript.ps1 ; Show-Balloon "Your script has finished." -duration 15 -Title "Hey, You!!" Displays a simple balloon popup for 15 seconds. .Example PS C:\> c:\scripts\myscript.ps1; Show-Balloon "There was an error with the script: $($error[0].Exception.Message)" -messagetype "Error" Display an error balloon tip showing the exception message from the last error PowerShell detected. .Example 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. .Inputs None .Outputs None .Link http://jdhitsolutions.com/blog/2009/11/the-powershell-balloon-festival .Link Write-Warning Write-Error .Notes NAME: Show-Balloon VERSION: 1.0 AUTHOR: Jeffery Hicks LASTEDIT: 11/12/2009 #> [CmdletBinding()] Param ( [Parameter( ValueFromPipeline=$False, Position=0, Mandatory=$True, HelpMessage="The message text to display. Keep it short and simple.")] [string]$message, [Parameter( ValueFromPipeline=$False, Mandatory=$False, HelpMessage="The message title")] [string]$Title="Attention $env:username", [Parameter( ValueFromPipeline=$False, Mandatory=$False, HelpMessage="The message type: Info,Error,Warning,None")] [ValidateSet("Info","Error","Warning","None")] [Alias("type")] [string]$MessageType="Info", [Parameter( ValueFromPipeline=$False, Mandatory=$False, HelpMessage="The number of seconds to display the message.")] [int]$duration=10 ) Write-Verbose "Starting Function" if ($verbosePreference -eq "Continue") { Write-Verbose "Getting Operating System" Write-Verbose "OS: $((get-wmiobject win32_operatingsystem).caption)" } Write-Verbose "Loading Assemblies" #load Windows Forms and drawing assemblies [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null #define an icon image pulled from PowerShell.exe Write-Verbose "Extracting the icon from PowerShell.exe" $icon=[system.drawing.icon]::ExtractAssociatedIcon((join-path $pshome powershell.exe)) Write-Verbose "Defining `$notify" $notify = new-object system.windows.forms.notifyicon $notify.icon = $icon $notify.visible = $True Write-Verbose "Evaluating MessageType parameter $messagetype" #define the tool tip icon based on the message type switch ($messagetype) { "Error" { $messageIcon=[system.windows.forms.tooltipicon]::Error} "Info" {$messageIcon=[system.windows.forms.tooltipicon]::Info} "Warning" {$messageIcon=[system.windows.forms.tooltipicon]::Warning} Default {$messageIcon=[system.windows.forms.tooltipicon]::None} } Write-Verbose "Displaying message [ $Message ] for $duration seconds" #display the balloon tipe $notify.showballoontip($duration,$Title,$message,$MessageIcon) Write-Verbose "Ending function" } #end function Set-Alias sb Show-Balloon # help Show-Balloon -full