Home > Enterprise >  How to open file on click envent notifyIcon in powershell?
How to open file on click envent notifyIcon in powershell?

Time:01-11

I'm facing an issue on my powershell script. I use a notifyIcon to display some error message for a monitoring script. This is work perfectly. But i want to open a log file when I click on the notification. Unfortunately this not work when I click on the notification. When I click on the notifyIcon nothing happen. I try many solutions but nothing until now.

This is my code:

     $global:balloon = New-Object System.Windows.Forms.NotifyIcon;
    
    $path = (Get-Process -id $pid).Path;
    $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path); 
    $balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning;
          
        
    $balloon.BalloonTipText = $msgAlert;
    $balloon.BalloonTipTitle = "Monitoring";
    $balloon.Visible = $true ;
$balloon_BalloonTipClicked={
   Start notepad $LogFileName;
}
    $balloon.ShowBalloonTip(5000);

Someone can help me please ? I tried many options like Add_click, MouseClick etc. Thank in Advance !

CodePudding user response:

Based from the comment posted by Scepticalist : Balloon Notifications with Powershell


Clear-Host
#Load the required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") #Remove any registered events related to notifications
Remove-Event BalloonClicked_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue
Remove-Event BalloonClosed_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue #Create the notification object
$notification = New-Object System.Windows.Forms.NotifyIcon 

#Define the icon for the system tray
$notification.Icon = [System.Drawing.SystemIcons]::Information

#Display title of balloon window
$notification.BalloonTipTitle = "This is a Balloon Title"

#Type of balloon icon
$notification.BalloonTipIcon = "Info"

#Notification message
$title = "This is the message in the balloon tip."
$notification.BalloonTipText = $title

#Make balloon tip visible when called
$notification.Visible = $True

## Register a click event with action to take based on event
#Balloon message clicked
register-objectevent $notification BalloonTipClicked BalloonClicked_event `
-Action {cmd /c Start "" /MAX Notepad} | Out-Null

#Balloon message closed
register-objectevent $notification BalloonTipClosed BalloonClosed_event `
-Action {[System.Windows.Forms.MessageBox]::Show("Balloon message closed","Information");$notification.Visible = $False} | Out-Null

#Call the balloon notification
$notification.ShowBalloonTip(5000)

EDIT : Creating a Balloon Tip Notification Using PowerShell


Clear-Host
Function Invoke-BalloonTip {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True,HelpMessage="The message text to display. Keep it short and simple.")]
        [string]$Message,

        [Parameter(HelpMessage="The message title")]
         [string]$Title="Attention $env:username",

        [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=5000
    )

    Add-Type -AssemblyName System.Windows.Forms
    #Remove any registered events related to notifications
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    Remove-Event BalloonClicked_event -ea SilentlyContinue
    Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue
    Remove-Event BalloonClosed_event -ea SilentlyContinue
    Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue

    #Create the notification object
    $global:balloon = New-Object System.Windows.Forms.NotifyIcon
    # Balloon Clicked_Event
    Register-objectevent $balloon BalloonTipClicked BalloonClicked_event -Action {cmd /c Start "" /MAX Notepad} | Out-Null

    # create an event handler which will be used to remove the system tray icon when it is double clicked
    [void](Register-ObjectEvent  -InputObject $balloon -EventName MouseDoubleClick -SourceIdentifier IconClicked  -Action {
    #Perform  cleanup actions on balloon tip
    $global:balloon.dispose()
    Unregister-Event  -SourceIdentifier IconClicked
    Remove-Job -Name IconClicked
    Remove-Variable  -Name balloon -Scope Global
    }) 

    #Need an icon for the tray
    $path = Get-Process -id $pid | Select-Object -ExpandProperty Path

    #Extract the icon from the file
    $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($SysTrayIconPath)

    #Can only use certain TipIcons: [System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property
    $balloon.BalloonTipIcon  = [System.Windows.Forms.ToolTipIcon]$MessageType
    $balloon.BalloonTipText  = $Message
    $balloon.BalloonTipTitle = $Title
    $balloon.Visible = $true

    #Display the tip and specify in milliseconds on how long balloon will stay visible
    $balloon.ShowBalloonTip($Duration)
    Write-Verbose "Ending function"

}

Invoke-BalloonTip -Message 'This is a  message from my function' -Title 'Attention!' -MessageType Info 

CodePudding user response:

I tried again but nothing when I cliked on my notification.

Clear-Host

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
Remove-Event BalloonClicked_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue
Remove-Event BalloonClosed_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue #Create the notification object


$global:balloon = New-Object System.Windows.Forms.NotifyIcon;
$balloon.Icon = [System.Drawing.SystemIcons]::Information
    $balloon.BalloonTipIcon = "Info";
    $balloon.BalloonTipText = $msgAlert;
    $balloon.BalloonTipTitle = "Monitoring";
    $balloon.Visible = $true ;

register-objectevent $balloon BalloonTipClicked BalloonClicked_event `
-Action {Start notepad $LogFileName;} | Out-Null

register-objectevent $balloon BalloonTipClosed BalloonClosed_event `
-Action {[System.Windows.Forms.MessageBox]::Show("Balloon message closed","Information");$notification.Visible = $False} | Out-Null

Maybe I miss something.

  • Related