事件是通过ISE触发的,但不在正常PS窗口中触发

时间:2018-08-14 10:24:31

标签: forms powershell events

我已经通过Google尝试了很多“在ise中但不在控制台中工作”链接,尤其是在Stackoverflow上,但可惜到目前为止我没有尝试过。

我将从整个代码开始:

.htaccess

现在,当其中一个事件被触发时(如单击气球,),然后调用“ ClearEvents”功能(出于测试目的,我添加了一个MsgBox)。

但是在控制台中,这些事件永远不会触发!为什么会这样?

经过Win7x64(PS 3.0)和Win10x64(PS 5.1)测试

我还用Param ( [ValidateRange(2,10)][int]$Time = 5, [ValidateLength(4,40)][String]$Title = "Attention $($Env:USERNAME.ToUpper())", [ValidateLength(4,40)][String]$Text = "This is a test!", [ValidateSet("None","Info","Warning","Error")] $Status = "Info", [ValidatePattern("[\\:\-\w\d\s]+.exe")][String]$TrayIcon = (Get-Process -id $pid).Path ) # sec to ms $TTL = $Time * 1000 if (![System.IO.File]::Exists($TrayIcon)){ Write-Host "File does not exist!" exit } function ClearEvents { # Perform cleanup actions on balloon tip $global:balloon.dispose() Remove-Variable -Name balloon -Scope Global Unregister-Event -SourceIdentifier TrayClicked Remove-Job -Name TrayClicked Unregister-Event -SourceIdentifier BallonClicked Remove-Job -Name BallonClicked Unregister-Event -SourceIdentifier BalloonClosed Remove-Job -Name BalloonClosed #test if function is called [System.Windows.MessageBox]::Show('Hello') } Add-Type -AssemblyName System.Windows.Forms $global:balloon = New-Object System.Windows.Forms.NotifyIcon # Tray-Icon $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($TrayIcon) # Balloon # [System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property $balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::$Status $balloon.BalloonTipTitle = $Title $balloon.BalloonTipText = $Text $balloon.Visible = $true $balloon.ShowBalloonTip($TTL) [void](Register-ObjectEvent -InputObject $balloon -EventName MouseClick -SourceIdentifier TrayClicked -Action { ClearEvents }) [void](Register-ObjectEvent -InputObject $balloon -EventName BalloonTipClicked -SourceIdentifier BallonClicked -Action { ClearEvents }) [void](Register-ObjectEvent -InputObject $balloon -EventName BalloonTipClosed -SourceIdentifier BalloonClosed -Action { ClearEvents }) 确认,两个控制台确实都在默认的“ STA”(单线程单元)下运行,显然某些“ System.Windows.Forms”需要正确运行。 不幸的是,这不是问题所在...

我的脚本基于以下脚本:https://mcpmag.com/articles/2017/09/07/creating-a-balloon-tip-notification-using-powershell.aspx

0 个答案:

没有答案