我已经在这个头上挠了好几个小时...下面的代码触发了单击或关闭通知气球的操作。我也是PowerShell的新手。
考虑以下代码:
####### Launch as : ##########################
## powershell.exe -sta -file .\balloon.ps1 ##
##############################################
Write-Host -ForeGround Yellow " ###### START OF SCRIPT ! ######"
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Title = "This is the title"
$Text = "This is the text"
$EventTimeOut = 5
$balloon = New-Object System.Windows.Forms.NotifyIcon
$balloon.Icon = [System.Drawing.SystemIcons]::Information
$balloon.BalloonTipTitle = $Title
$balloon.BalloonTipText = $Text
$balloon.Visible = $True
$balloon.ShowBalloonTip(1)
Register-ObjectEvent $balloon BalloonTipClicked -SourceIdentifier event_BalloonTipClicked `
-Action {
# explorer.exe; `
Write-Host -ForeGround Green "event_BalloonTipClicked occured !"; `
# Gets rid of icon
$balloon.Dispose(); `
}|Out-Null
Register-ObjectEvent $balloon BalloonTipClosed -SourceIdentifier event_BalloonTipClosed `
-Action {
Write-Host -ForeGround Green "event_BalloonTipClosed occured !"; `
$balloon.Dispose(); `
}|Out-Null
Wait-Event event_BalloonTipClicked -TimeOut $EventTimeOut
Wait-Event event_BalloonTipClosed -TimeOut $EventTimeOut
Unregister-Event -SourceIdentifier event_BalloonTipClicked
Unregister-Event -SourceIdentifier event_BalloonTipClosed
Write-Host -ForeGround Gray "Should be empty -- start --"
Get-EventSubscriber
Write-Host -ForeGround Gray "Should be empty -- end --"
#[System.Windows.Forms.MessageBox]::Show("Done !!")
Write-Host -ForeGround Yellow " ###### END OF SCRIPT ! ######"
我希望脚本在“Register-ObjectEvent”完成触发其 Action 后立即结束。
但是,返回仅在“等待事件”中指定的超时之后发生,从而阻止了代码的进一步执行。这种行为也使我无法将代码转换为函数。
如果脚本仅侦听一个事件,则其行为方式相同。
任何帮助将非常感激 !
最佳答案
您的时间的战车Shay Levy。我没有设法正确使用您的解决方案。
我使用此vbscript在后台启动文件。
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "powershell.exe -NoExit -Sta -File .\balloon.ps1",0
不幸的是,例如启动脚本5次将使我5个正在运行的Powershell实例无所作为。
我终于意识到,将“Register-ObjectEvent -Action”和“Wait-Event”混合在一起绝对是不可以的(http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/10983ec3-7aa6-4011-a87e-a30a25ab484a/)
以下代码是我的目标。这是解决问题的同步方法。
###################################################
## Launch as : ##
## cmd /k powershell -Sta [-File] .\balloon.ps1 ##
###################################################
# This post put me on the right track "http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/10983ec3-7aa6-4011-a87e-a30a25ab484a/"
Write-Host -ForeGround Yellow " ###### START OF SCRIPT ! ######"
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Title = "This is the title"
$Text = "This is the text"
$EventTimeOut = 5
$balloon = New-Object System.Windows.Forms.NotifyIcon -Property @{
Icon = [System.Drawing.SystemIcons]::Information
BalloonTipTitle = $Title
BalloonTipText = $Text
Visible = $True
}
# Value "1" here is meaningless. $EventTimeOut will force bubble to close.
$balloon.ShowBalloonTip(1)
Register-ObjectEvent $balloon BalloonTipClicked -SourceIdentifier event_BalloonTipClicked
Register-ObjectEvent $balloon BalloonTipClosed -SourceIdentifier event_BalloonTipClosed
# "Wait-Event" pauses the script here until an event_BalloonTip* is triggered
# TimeOut is necessary or balloon and script hangs there forever.
# This could be okay but event subscription gets messed up by following script instances generating the same event names1.
$retEvent = Wait-Event event_BalloonTip* -TimeOut $EventTimeOut
# Script resumes here.
$retSourceIdentifier = $retEvent.SourceIdentifier
If ($retSourceIdentifier -eq $null){
Write-Host -ForeGround Green "TimeOut occured !"
}Else{
Write-Host -ForeGround Green "$retSourceIdentifier occured !"
}
If ($retSourceIdentifier -eq "event_BalloonTipClicked"){
explorer.exe
}
# Gets rid of icon. This is absolutely necessary, otherwise icon is stuck event if parent script/shell closes
$balloon.Dispose()
# Tidy up, This is needed if returning to parent shell.
Unregister-Event -SourceIdentifier event_BalloonTip*
Get-Event event_BalloonTip* | Remove-Event
Write-Host -ForeGround Gray "Should be empty -- start --"
Get-EventSubscriber
Write-Host -ForeGround Gray "Should be empty -- end --"
#[System.Windows.Forms.MessageBox]::Show("Done !!")
Write-Host -ForeGround Yellow " ###### END OF SCRIPT ! ######"
关于powershell - PowerShell,当 “Wait-Event”触发时从 “Register-ObjectEvent -Action”返回吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7350834/