Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在powershell中拦截WM_退出?_Powershell_Windows Server 2008 R2 - Fatal编程技术网

如何在powershell中拦截WM_退出?

如何在powershell中拦截WM_退出?,powershell,windows-server-2008-r2,Powershell,Windows Server 2008 R2,我必须维护一个由主应用程序(控制面板)组成的系统,该系统可根据需要启动/停止辅助应用程序。辅助应用程序作为单独的进程运行,根据文档,当主机希望它们停止时,它会向它们发送WM_退出信号,如果应用程序不停止,它会终止进程 辅助应用程序之一是运行无窗口的powershell脚本。有没有办法在powershell中拦截WM_QUIT?我在谷歌上搜索了一个要注册到register ObjectEvent的事件,但什么也没找到 目前,系统运行在windows 2008-R2+Powershell v2上,但

我必须维护一个由主应用程序(控制面板)组成的系统,该系统可根据需要启动/停止辅助应用程序。辅助应用程序作为单独的进程运行,根据文档,当主机希望它们停止时,它会向它们发送WM_退出信号,如果应用程序不停止,它会终止进程

辅助应用程序之一是运行无窗口的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.
}

WM_QUIT要求您有一个窗口。WM=窗口消息。如果需要,可以隐藏窗口,但您需要一个窗口。你是在试图倾听事件的发生,还是在试图阻止事件的发生?我做了一些测试。不考虑Visible属性,必须在屏幕外创建窗口,只要[System.Windows.Forms.Application]:Run($Form)未实例化窗口,则不会执行onLoad过程:运行($Form),在执行onLoad代码期间不会检查事件,一旦调用Application.Run,脚本不能做任何其他事情。但是我有了这个想法,并且能够让我的脚本WM_退出。