【问题标题】:Permanent WMI Event consumer doesnt get triggered, temporary does永久 WMI 事件使用者不会被触发,临时会
【发布时间】:2014-11-24 15:06:25
【问题描述】:

我正在尝试创建一个永久 WMI 事件订阅,以便在连接 USB 驱动器时运行脚本。

这是我创建过滤器、消费者和绑定的方法:

$computer = "xxx"
$filterNS = "root\cimv2"
$wmiNS = "root\subscription"
$query = "Select * from __InstanceCreationEvent within 5 where targetinstance isa 'win32_logicaldisk'"

$filterName = "TestFilter"

$filterPath = Set-WmiInstance -Class __EventFilter `
 -ComputerName $computer -Namespace $wmiNS -Arguments `
  @{name=$filterName; EventNameSpace=$filterNS; QueryLanguage="WQL";
    Query=$query}

$consumerPath = Set-WmiInstance -Class CommandLineEventConsumer `
 -ComputerName $computer -Namespace $wmiNS `
 -Arguments @{
 name="TestConsumer"; 
 ExecutablePath= "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
 CommandLineTemplate = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -executionpolicy bypass -file D:\\reassignDriveletter.ps1"
 }

Set-WmiInstance -Class __FilterToConsumerBinding -ComputerName $computer `
  -Namespace $wmiNS -arguments @{Filter=$filterPath; Consumer=$consumerPath} |
  out-null

一切都创建时没有错误,我可以在 WMI 事件查看器中看到过滤器、消费者和绑定,但如果我连接 USB 驱动器,则没有任何反应(脚本的第一行写了一个日志条目,这就是我所知道的)。

出于测试目的,我创建了一个正常的事件订阅,如下所示:

$job = Register-WmiEvent -Query "Select * from __InstanceCreationEvent within 5 where targetinstance isa 'win32_logicaldisk'" -SourceIdentifier usb -Timeout 1000 -Action $scriptblock

效果很好。

我是否遗漏了一些明显的东西?如有任何帮助,我将不胜感激。

问候

更新:刚刚在未加入域的 Win7 计算机上进行了测试,相同的代码可以正常工作。 (我的工作站是Win8.1加入域的记录)。我将在目标系统上进行测试并报告。

【问题讨论】:

  • 这只是一个赃物,但您是否尝试过删除 -Arguments 变量并改用硬编码值,例如-arguments @{name="TestFilter"; EventNameSpace="root\cimv2";...} 我只是想知道是否应该在 name="$filtername" 这样的引号中指定参数。此外,如果您从 C# 中提取此示例,则可能不需要在 ExecutablePath 和 CommandLineTemplate 的路径中使用双反斜杠。反斜杠不是 PowerShell 中的转义字符。
  • @KeithHill 刚刚使用硬编码值进行了测试,没有双反斜杠,但这也无济于事:/ 变量也不是问题,因为我可以看到 wmi 查看器中的值是正确的

标签: windows events powershell wmi


【解决方案1】:

好的,经过几天的反复试验,我最终适应/使用了另一种方法 here

这将在每次连接 USB 时启动存储在 D:\scripts 中的脚本,并且它是永久性的。

$filter = ([wmiclass]"\\.\root\subscription:__EventFilter").CreateInstance()

$filter.QueryLanguage = "WQL"
$filter.Query = "Select * from __InstanceCreationEvent within 5 where targetinstance isa 'win32_logicaldisk'"
$filter.Name = "USBFilter"
$filter.EventNamespace = 'root\cimv2'

$result = $filter.Put()
$filterPath = $result.Path

$consumer = ([wmiclass]"\\.\root\subscription:CommandLineEventConsumer").CreateInstance()
$consumer.Name = 'USBConsumer'
$consumer.CommandLineTemplate = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe –ExecutionPolicy Bypass -file D:\scripts\reassignDriveletter.ps1"
$consumer.ExecutablePath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$consumer.WorkingDirectory = "D:\scripts"
$result = $consumer.Put()
$consumerPath = $result.Path

$bind = ([wmiclass]"\\.\root\subscription:__FilterToConsumerBinding").CreateInstance()

$bind.Filter = $filterPath
$bind.Consumer = $consumerPath
$result = $bind.Put()
$bindPath = $result.Path

要删除这些永久事件,请执行以下操作:

([wmi]$filterPath).Delete()
([wmi]$consumerPath).Delete()
([wmi]$bindPath).Delete()

每次插入 USB 驱动器时,我的测试脚本都会创建一个文件夹,因此我可以对其进行测试并且它可以正常工作。

我正在运行 Windows 8.1 顺便说一句。

【讨论】:

  • 嗨米奇,谢谢你的努力。我刚刚对此进行了测试,我开始怀疑我的工作站存在问题,因为您的方法也不起作用(如我的更新中所述,我的脚本实际上可以在另一台机器上运行)。我还没有在目标机器上测试任何脚本,所以在那之前我想它们都可以正常工作
  • 我刚刚看到了那个更新。顺便说一句,如果您使用域帐户创建事件,则需要考虑安全性。基本上,该帐户需要是本地管理员组的成员,更多信息在这里:msdn.microsoft.com/en-us/library/aa393016(v=vs.85).aspx
  • 是的,我看到了,我创建事件的帐户是域和本地管理员。我也尝试使用机器本地管理员帐户来创建它们,但也没有运气
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 2011-11-19
相关资源
最近更新 更多