我必须维护一个由主应用程序(控制面板)组成的系统,该系统可以按需启动/停止辅助应用程序。辅助应用程序作为单独的进程运行,并且根据文档,当主服务器希望它们停止时,它将向它们发送WM_QUIT信号,如果应用程序不停止,则将终止该进程。
辅助应用程序之一是运行无窗口的Powershell脚本。有没有办法在Powershell中拦截WM_QUIT?我用谷歌搜索了一个要向Register-ObjectEvent
注册的事件,但未找到任何内容。
目前,该系统在Windows 2008-R2 + Powershell v2上运行,但如有必要,我可以升级到Windows 2012-R2 + Powershell v5。
这将是一个隐藏的表格。如果将WM_quit发送给它,它将监听。
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.text = "Form"
$Form.TopMost = $false
$Form.Visible = $false
$Form.Add_Load({ test })
Function test () {
# Start Writing your code here
# This hidden Window will receive your WM_Quit
# Since your code is part of the form it will stop when WM_Quit is sent.
}