关于 ISE 与带有 SystemEvents 的控制台的问题
Question about ISE vs Console with SystemEvents
当我 运行 在 PowerShell ISE 中执行以下操作时,它运行完美,给出了 "AccountLock" 或 "AccountUnlock" 的原因,完全符合预期。但是,当我在提升的 powershell 控制台中 运行 这个确切的命令时,它根本不会 return 控制台中的 sessionswitch 原因。解锁后return什么都没有。
我检查了 Get-EventSubscriber
和 Get-Job
,看起来都已成功创建。
订阅者和工作的屏幕截图:
Register-ObjectEvent -InputObject $([microsoft.win32.systemevents]) -EventName "SessionSwitch" -Action {write-host $event.SourceEventArgs.Reason}
我想做的一件事是 windows 检测会话何时解锁(在用户将其密码与域同步后)并打开程序。
OS: Windows 10
版本:5.1 内部版本 17134 R 590
查看 Microsoft 文档:SystemEvents.SessionSwitch Event,解释似乎是此消息被发送到消息泵,消息泵是处理图形消息的代码的一部分。您或许可以尝试在代码中使用隐藏表单来强制创建消息泵。
Note :
This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class.
四处寻找后,我找不到使用 [windows.win32.systemevents] 的好方法,所以我联系了 Lee Holmes。他说是因为powershell console host默认是单线程单元(STA)模式的运行s。如果你 运行 它在 MTA 中,它工作正常。
最简单的解决方法是在脚本开头使用与此类似的代码以确保您处于 MTA 模式,如果未处于 MTA 模式则启动新的 powershell 进程并重新加载脚本。
if ([System.Threading.Thread]::CurrentThread.ApartmentState -ne [System.Threading.ApartmentState]::MTA)
{
powershell.exe -MTA -File $MyInvocation.MyCommand.Path
return
}
当我 运行 在 PowerShell ISE 中执行以下操作时,它运行完美,给出了 "AccountLock" 或 "AccountUnlock" 的原因,完全符合预期。但是,当我在提升的 powershell 控制台中 运行 这个确切的命令时,它根本不会 return 控制台中的 sessionswitch 原因。解锁后return什么都没有。
我检查了 Get-EventSubscriber
和 Get-Job
,看起来都已成功创建。
订阅者和工作的屏幕截图:
Register-ObjectEvent -InputObject $([microsoft.win32.systemevents]) -EventName "SessionSwitch" -Action {write-host $event.SourceEventArgs.Reason}
我想做的一件事是 windows 检测会话何时解锁(在用户将其密码与域同步后)并打开程序。
OS: Windows 10 版本:5.1 内部版本 17134 R 590
查看 Microsoft 文档:SystemEvents.SessionSwitch Event,解释似乎是此消息被发送到消息泵,消息泵是处理图形消息的代码的一部分。您或许可以尝试在代码中使用隐藏表单来强制创建消息泵。
Note :
This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class.
四处寻找后,我找不到使用 [windows.win32.systemevents] 的好方法,所以我联系了 Lee Holmes。他说是因为powershell console host默认是单线程单元(STA)模式的运行s。如果你 运行 它在 MTA 中,它工作正常。
最简单的解决方法是在脚本开头使用与此类似的代码以确保您处于 MTA 模式,如果未处于 MTA 模式则启动新的 powershell 进程并重新加载脚本。
if ([System.Threading.Thread]::CurrentThread.ApartmentState -ne [System.Threading.ApartmentState]::MTA)
{
powershell.exe -MTA -File $MyInvocation.MyCommand.Path
return
}