Why we need Balloon Tip Notification?
Mostly you will use common notification like display message in console using Write-* cmdlets to write the text to the console and Commonlyย we use Write-Error or Write-Warning for displaying Error and warnings.
Apart from displaying the message through the console, sometimes we required to get the user’s attention when something has completedย or an issue is encountered. In that case, a pop-up window will helps you to built and displayed to the information/Warning/Error to the User.ย In this type of situation, we can implement the Balloon Tool Tip notification.
To achieve this, we need to work with the System.Windows.Forms.NotifyIcon type to build our notification balloon tip.
STEP 1# :
To get started, you need to load up an assembly to take advantage of a method that will help to extract the icon image from the file.
Add-Type -AssemblyName System.Windows.Forms
STEP 2# :
Second, you need to add the System.Windows.Forms assembly into our PowerShell session before we can make use of the NotifyIcon class. So your function would have to check whether there is already an icon that you can reuse.ย ย This is done by using a “shared variable”, which really is a variable that has “script:” scope.ย Shared variables will be active their value as long as the script runs.
if ($script:balloonToolTip -eq $null)
{
$script:balloonToolTip = New-Object System.Windows.Forms.NotifyIcon
}
STEP 3# :
Then you want to set the system tray icon of the PowerShell ISE by just locating its path via Get-Process and locating the Path property. After you have the path, then you need to extract the icon from the file so it can be applied to the Icon property.Using this approach, you can pull any icon from a file and use it for the system tray icon when the balloon tip is launched.
As per above statement, the Get-Process -id $pid command gets the PowerShell process that is hosting the current session.The ExpandProperty expands the collections, that is, it will output theย properties of every object in the collection. For example the out of $path variable will be “C:\windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe”
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
STEP 4# :
The ExtractAssociatedIcon which comes with the System.Drawing.Icon class and accepts a parameter of a string path name.
$balloonToolTip.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
Full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
function ShowBalloonTipInfo { [CmdletBinding()] param ( [Parameter()] $Text, [Parameter()] $Title, #It must be 'None','Info','Warning','Error' $Icon = 'Info' ) Add-Type -AssemblyName System.Windows.Forms #So your function would have to check whether there is already an icon that you can reuse.This is done by using a "shared variable", which really is a variable that has "script:" scope. if ($script:balloonToolTip -eq $null) { #we will need to add the System.Windows.Forms assembly into our PowerShell session before we can make use of the NotifyIcon class. $script:balloonToolTip = New-Object System.Windows.Forms.NotifyIcon } $path = Get-Process -id $pid | Select-Object -ExpandProperty Path $balloonToolTip.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) $balloonToolTip.BalloonTipIcon = $Icon $balloonToolTip.BalloonTipText = $Text $balloonToolTip.BalloonTipTitle = $Title $balloonToolTip.Visible = $true #I thought to display the tool tip for one seconds,so i used 1000 milliseconds when I call ShowBalloonTip. $balloonToolTip.ShowBalloonTip(1000) } ShowBalloonTipInfo ("The Notification from Dotnet-helpers : ","Read the latest topics of poweshell from dotnehelpers.com") |
Leave A Comment