Skip to content

Instantly share code, notes, and snippets.

@Nora-Ballard
Created February 15, 2014 13:53
Show Gist options
  • Select an option

  • Save Nora-Ballard/9019605 to your computer and use it in GitHub Desktop.

Select an option

Save Nora-Ballard/9019605 to your computer and use it in GitHub Desktop.
##Requires -Version 2.0
function New-TrayNotifyIcon
{
param(
[parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[Alias('Icon')]
$IconPath = "$env:windir\system32\WindowsPowerShell\v1.0\powershell.exe",
[parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
$Title,
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
$Text,
[parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[ValidateSet('None','Info','Warning','Error')]
$Level = 'Info',
[parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
$Delay = 10,
[parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
$Link
)
BEGIN
{
$NotifyIcon_BalloonTipText_maxLength = 255
$NotifyIcon_BalloonTipTitle_maxLength = 63
# This Requires -STA
$IsSTA = [System.Threading.Thread]::CurrentThread.ApartmentState -eq "STA"
if( !$IsSTA )
{
Throw "Notification Icons cannot be displayed in MTA mode. (Must run PowerShell with -STA switch to enable)"
}
#[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Write-Debug
#[reflection.assembly]::loadwithpartialname("System.Drawing") | Write-Debug
Reset-Tray
$DEFAULT_ICON_PATH = "$env:windir\system32\WindowsPowerShell\v1.0\powershell.exe"
}
PROCESS
{
$TipIcon = [system.windows.forms.tooltipicon]::$Level
if ( !(test-path -path $iconPath -pathtype leaf) )
{
Write-Error "File '$iconPath' not found."
$iconpath = $DEFAULT_ICON_PATH
}
try { $Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($iconPath) }
catch
{
try { $Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($DEFAULT_ICON_PATH) }
catch { throw "The notify icon must have an icon file." }
}
$NotifyIcon = New-Object system.windows.forms.notifyicon
$NotifyIcon.icon = $Icon
# Clear any previous events
$NotifyIcon_BalloonTip_Clicked = "NotifyIcon.BallonTipClicked"
$NotifyIcon_BalloonTip_Closed = "NotifyIcon.BallonTipClosed"
Remove-Event $NotifyIcon_BalloonTip_Clicked -ErrorAction SilentlyContinue
Remove-Event $NotifyIcon_BalloonTip_Closed -ErrorAction SilentlyContinue
# Register Events
Register-ObjectEvent -InputObject $NotifyIcon -EventName BalloonTipClicked -SourceIdentifier $NotifyIcon_BalloonTip_Clicked
Register-ObjectEvent -InputObject $NotifyIcon -EventName BalloonTipClosed -SourceIdentifier $NotifyIcon_BalloonTip_Closed
try
{
$NotifyIcon.BalloonTipTitle = $Title ; Write-Debug $( "Balloon Tip Title:`t {0}" -f $NotifyIcon.BalloonTipTitle )
$NotifyIcon.BalloonTipText = $Text ; Write-Debug $( "Balloon Tip Text:`t {0}" -f $NotifyIcon.BalloonTipText )
$NotifyIcon.BalloonTipIcon = $TipIcon ; Write-Debug $( "Balloon Tip Level:`t {0}" -f $NotifyIcon.BalloonTipIcon )
$NotifyIcon.visible = $true ; Write-Debug $( "Notification Icon:`t {0}" -f $NotifyIcon.visible )
$NotifyIcon.showballoontip($delay) ; Write-Debug $( "Balloon Tip Show:`t {0} seconds" -f $Delay )
$StartTime = get-date
:wait_event while ($True)
{
Write-Debug 'Wait for Event'
$event = Wait-Event
if ($event) { Write-Debug 'Event Raised' }
switch ($event.SourceIdentifier)
{
$NotifyIcon_BalloonTip_Clicked { Write-Debug "Balloon Tip Event:`t Clicked"
if ($link)
{
$NotifyIcon.visible = $false
Start-Process $Link ; Write-Debug "Balloon Tip Action:`t start-process $link"
start-sleep 10
}
Break wait_event
}
$NotifyIcon_BalloonTip_Closed { Write-Debug "Balloon Tip Event:`t Closed"
Start-Sleep 1
Write-Debug $( "Total Time Displayed:`t {0}" -f ((Get-Date) - $StartTime).TotalSeconds )
Return
}
}
}
}
finally
{
Write-Debug "NotifyIcon: Cleanup"
# Close Notify Icon
$NotifyIcon.visible = $false
$NotifyIcon.Dispose()
# Clear Events
Remove-Event $NotifyIcon_BalloonTip_Clicked -ErrorAction SilentlyContinue
Remove-Event $NotifyIcon_BalloonTip_Closed -ErrorAction SilentlyContinue
Unregister-Event -SourceIdentifier $NotifyIcon_BalloonTip_Clicked -ErrorAction SilentlyContinue
Unregister-Event -SourceIdentifier $NotifyIcon_BalloonTip_Closed -ErrorAction SilentlyContinue
Reset-Tray
Write-Output $_
}
}
END
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment