ex_unit_notifier
ex_unit_notifier copied to clipboard
Add Notifications for Windows
On Windows the following PowerShell script can be used for creating a tray notification:
Invoke-Notification.ps1
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Function Invoke-Notification {
<#
.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 Title
The title for the message balloon.
.Parameter Message
The message text you want to display. Recommended to keep it short and simple.
.Parameter MessageType
The type of message. This value determines what type of icon to display. Valid values are:
Info,Error,Warning,None
.Parameter SysTrayIcon
The path to a file that you will use as the system tray icon. Default is the PowerShell ISE icon.
.Parameter Duration
The number of seconds to display the balloon popup. The default is 1000.
.Inputs
None
.Outputs
None
.Notes
NAME: Invoke-BalloonTip
VERSION: 1.1
AUTHOR: Marduk Bolaños
HISTORY: Based on the original by Boe Prox.
#>
[CmdletBinding()]
Param (
[Parameter(HelpMessage="The message title")]
[string]$Title="Attention $env:username",
[Parameter(Mandatory=$True,HelpMessage="The message text to display. Keep it short and simple.")]
[string]$Message,
[Parameter(HelpMessage="The message type: Info,Error,Warning,None")]
[System.Windows.Forms.ToolTipIcon]$MessageType="Info",
[Parameter(HelpMessage="The path to a file to use its icon in the system tray")]
[string]$SysTrayIconPath='C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe',
[Parameter(HelpMessage="The number of milliseconds to display the message.")]
[int]$Duration=1000
)
Add-Type -AssemblyName System.Windows.Forms
If (-NOT $global:balloon) {
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
}
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($SysTrayIconPath)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]$MessageType
$balloon.BalloonTipText = $Message
$balloon.BalloonTipTitle = $Title
$balloon.Visible = $true
$balloon.ShowBalloonTip($Duration)
Write-Verbose "Ending function"
}
$cmd, $params = $args
& $cmd @params
The script can be executed with:
powershell.exe .\path\to\Invoke-Notification.ps1 Invoke-Notification Title Message
Caveats
- Notifications have to be enabled. In an enterprise environment they might be disabled by a company-wide policy.
- The user may have to change the ExecutionPolicy to e.g. RemoteSigned. However, I tried this script with the default policy, Restricted, and it works. So it shouldn't be necessary to change it.