Events powershell 2事件处理

Events powershell 2事件处理,events,powershell,Events,Powershell,我想处理System.Windows.Forms.NotifyIcon的BallootTipClick。也就是说,我希望在单击提示时处理事件。我的代码如下,但我无法捕获事件。请帮忙 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Timers") ##

我想处理System.Windows.Forms.NotifyIcon的BallootTipClick。也就是说,我希望在单击提示时处理事件。我的代码如下,但我无法捕获事件。请帮忙

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Timers")

## This is the location of your download files
$notification = "E:\TDdownload"

$notification = New-Object System.Windows.Forms.NotifyIcon 

$notification.Icon = "C:\Users\Sefler\Desktop\PerfCenterCpl.ico"
$notification.BalloonTipIcon = "Info" 
$notification.BalloonTipText = "Windows will now try to clean "+ $fileLocation +" as scheduled." 
$notification.BalloonTipTitle = "Windows auto maintaince"

$notification.Visible = $True 
$notification.ShowBalloonTip(15000)

## Register a click event
register-objectevent $notification BalloonTipClicked -sourceIdentifier notification_event

## Wait for the onClick event
wait-event -timeout 15 

好的,我现在和你在一起。这在ISE内部起作用:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Timers")

## This is the location of your download files 
$notification = "E:\TDdownload" 

$notification = New-Object System.Windows.Forms.NotifyIcon

$notification.Icon = "C:\Users\Sefler\Desktop\PerfCenterCpl.ico"
$notification.BalloonTipTitle = "Windows auto maintaince"
$notification.BalloonTipIcon = "Info"
$title = "Windows will now try to clean {0} as scheduled." -f $fileLocation
$notification.BalloonTipText = $title
$notification.Visible = $True
## Clear any previous events
Remove-Event notification_event -ea SilentlyContinue
## Register a click event
register-objectevent $notification BalloonTipClicked notification_event 
$notification.ShowBalloonTip(15000) 

## Wait for the onClick event 
wait-event -timeout 15 -sourceIdentifier notification_event > $null
Remove-Event notification_event -ea SilentlyContinue

"Done!!"

Unregister-Event -SourceIdentifier notification_event

注意,当您在窗口主体中单击时,此功能起作用,但当您单击“x”关闭窗口时,此功能不起作用。因此,您可能还想订阅BallootTipClosed事件(或代替BallootTipClicked)。

我已经提到了这一点,但问题是它无法捕获该事件。正如我的代码所示,一旦偶数发生,脚本将不会等待15秒。但我总是等15秒。这让我发疯!!!!!当我试着在PowerShell ISE中运行你和我的vesion时。两种方法都有效!!但是,当我在正常的PowerShell窗口中运行它们时,它们都不工作!我在两台电脑上试过。一个是Windows7Pro,另一个是Vista Home Basic。我就是不明白为什么!尝试使用选项-STA.FYI,ISE启动Powershell.exe,因为它是一个GUI应用程序,默认情况下使用单线程单元(STA),而控制台主机默认情况下使用多线程单元(MTA),除非在启动Powershell.exe时指定-STA。