Skip to content

Instantly share code, notes, and snippets.

@Graham-Beer
Created February 13, 2019 17:34
Show Gist options
  • Save Graham-Beer/2551db9c2f0c5946cfe3563471de9c25 to your computer and use it in GitHub Desktop.
Save Graham-Beer/2551db9c2f0c5946cfe3563471de9c25 to your computer and use it in GitHub Desktop.
function Invoke-BalloonMessage {
Param (
[Parameter(Mandatory)]
[string] $Message,
[Parameter()]
[string] $Title = "Attention $env:username",
[Parameter()]
[ValidateSet('none', 'Info', 'Warning', 'Error')]
[string] $MessageType,
[Parameter()]
[string] $SysTrayIconPath = 'C:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe',
[Parameter()]
[int] $Duration = 10000
)
begin {
Add-Type -AssemblyName System.Windows.Forms
}
process {
$global:balloon = [System.Windows.Forms.NotifyIcon]@{
Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($SysTrayIconPath)
BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]$MessageType
BalloonTipText = $Message
BalloonTipTitle = $Title
Visible = $True
}
$balloon.ShowBalloonTip($Duration)
}
end {
if ($balloon) {
$params = @{
InputObject = $balloon
EventName = 'BalloonTipClosed'
SourceIdentifier = 'BalloonClosure'
Action = {
$balloon.dispose()
Unregister-Event -SourceIdentifier BalloonClosure
Remove-Job -Name BalloonClosure
Get-Variable -Name balloon -Scope global | Remove-Variable -Force
}
}
$null = Register-ObjectEvent @params
}
}
}
# Invoke-BalloonMessage -Message "Test" -MessageType Info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment