【问题标题】:WMI Event Subscription and PowerShell executionWMI 事件订阅和 PowerShell 执行
【发布时间】:2017-11-03 22:12:46
【问题描述】:

当某个事件发生时,我需要启动一个 PowerShell 脚本,并且我正在使用 WMI 类来获得持久性。我只能让它部分工作,需要一些帮助才能使其完全工作。所以,这里是什么有效,什么不...

以下代码有效,并且会在启动 calc.exe 时在后台启动 PowerShell(为了简单起见,我选择此事件只是为了测试目的)。

$fname = "testFilter"
$cname="testConsumer"
$exePath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$query="SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='calc.exe'"
$WMIEventFilter=Set-WmiInstance -Class __EventFilter -Namespace "root\subscription" -Arguments @{Name=$fname;EventNameSpace="root\cimv2";QueryLanguage="WQL";Query=$query}
$WMIEventConsumer=Set-WmiInstance -Class CommandLineEventConsumer -Namespace "root\subscription" -Arguments @{Name=$cname;ExecutablePath=$exePath}
Set-WmiInstance -Class __FilterToConsumerBinding -Namespace "root\subscription" -Arguments @{Filter=$WMIEventFilter;Consumer=$WMIEventConsumer} | out-null

但是,如果我修改 $exePath 变量以将参数传递给 powershell.exe,那么它就不再起作用(不会创建任何 powershell 进程)。

我还尝试将CommandLineEventConsumer 替换为ActiveScriptEventConsumer,并使用VBScript 启动powershell。这是修改后的代码(只有第 3 行和第 5 行不同):

$fname = "testFilter"
$cname="testConsumer"
$scriptPath="D:\Work\LaunchPowerShell.vbs"
$query="SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='calc.exe'"
$WMIEventFilter=Set-WmiInstance -Class __EventFilter -Namespace "root\subscription" -Arguments @{Name=$fname;EventNameSpace="root\cimv2";QueryLanguage="WQL";Query=$query}
$WMIEventConsumer=Set-WmiInstance -Class ActiveScriptEventConsumer -Namespace "root\subscription" -Arguments @{Name=$cname;ScriptFileName=$scriptPath;ScriptingEngine="VBScript"}
Set-WmiInstance -Class __FilterToConsumerBinding -Namespace "root\subscription" -Arguments @{Filter=$WMIEventFilter;Consumer=$WMIEventConsumer} | out-null

还有 LaunchPowerShell.vbs:

Dim objShell : Set objShell = WScript.CreateObject("WScript.shell")
objShell.run("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe D:\Work\MyScript.ps1")

VB 脚本在从命令提示符 (cmd.exe) 启动时会按预期工作,但在触发事件时(即启动 calc.exe 时)运行 powershell 就没有运气了。即使我从 powershell 参数中删除我的脚本,它也不会运行,所以不确定是什么问题。

如果有人可以提供帮助,将不胜感激。谢谢!!!

【问题讨论】:

标签: powershell vbscript wmi


【解决方案1】:

如果您指定 CommandLineTemplate 而不是 ExecutablePath,则可以向字符串添加参数。

$fname = "testFilter"
$cname = "testConsumer"
$CommandLineTemplate = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File D:\Work\MyScript.ps1"
$ExecutablePath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$query = "SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='calc.exe'"

$WMIEventFilter = Set-WmiInstance -Class __EventFilter -Namespace "root\subscription" -Arguments @{Name=$fname;EventNameSpace="root\cimv2";QueryLanguage="WQL";Query=$query}
$WMIEventConsumer = Set-WmiInstance -Class CommandLineEventConsumer -Namespace "root\subscription" -Arguments @{Name=$cname;CommandLineTemplate=$CommandLineTemplate;ExecutablePath=$ExecutablePath }

Set-WmiInstance -Class __FilterToConsumerBinding -Namespace "root\subscription" -Arguments @{Filter=$WMIEventFilter;Consumer=$WMIEventConsumer} | out-null

Source:

命令行模板

数据类型:字符串

访问类型:只读

指定要启动的进程的标准字符串模板。该属性可以为NULL,ExecutablePath属性作为命令行使用。

【讨论】:

  • 谢谢肖恩。我一定忽略了这个属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多