如何在powershell中单击envent notifyIcon打开文件?

yk9xbfzb  于 2023-01-30  发布在  Shell
关注(0)|答案(1)|浏览(141)

我在powershell脚本上遇到了一个问题。我使用一个notifyIcon来显示监视脚本的一些错误消息。这是完美的工作。但我想打开一个日志文件时,我点击通知。不幸的是,这不工作时,我点击通知。当我点击notifyIcon什么也没有发生。我尝试了许多解决方案,但直到现在什么也没有。
这是我的代码:

$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);

有人可以帮助我吗?我尝试了许多选项,如添加_单击,鼠标单击等,提前感谢!
我又试了一次,但当我点击我的通知。

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

也许我错过了什么。

lzfw57am

lzfw57am1#

基于***怀疑论者***发表的评论: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)

编辑:***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

相关问题