Powershell WMI永久事件

Powershell WMI永久事件,powershell,events,wmi,Powershell,Events,Wmi,我尝试根据来自Windows7SP1网卡的特定事件设置wmi永久事件 我使用以下代码: #Creating a new event filter $instanceFilter = ([wmiclass]"\\.\root\subscription:__EventFilter").CreateInstance() $instanceFilter.QueryLanguage = "WQL" $instanceFilter.Query = "Select * From __InstanceC

我尝试根据来自Windows7SP1网卡的特定事件设置wmi永久事件

我使用以下代码:

    #Creating a new event filter
$instanceFilter = ([wmiclass]"\\.\root\subscription:__EventFilter").CreateInstance()
$instanceFilter.QueryLanguage = "WQL"
$instanceFilter.Query = "Select * From __InstanceCreationEvent Where TargetInstance ISA 'Win32_NTLogEvent' AND TargetInstance.LogFile='System' AND TargetInstance.SourceName LIKE '%e1_express%' AND (TargetInstance.EventCode=27 OR TargetInstance.EventCode=32 OR TargetInstance.EventCode=33 OR TargetInstance.EventCode=36)"
$instanceFilter.Name = "LAN_Watcher_Filter"
$instanceFilter.EventNamespace = 'root\cimv2'
$result = $instanceFilter.Put()
$newFilter = $result.Path


#Creating a new event consumer

$instanceConsumer = ([wmiclass]"\\.\root\subscription:CommandLineEventConsumer").CreateInstance()
$instanceConsumer.Name ='LAN_Watcher_Consumer'
$instanceConsumer.ExecutablePath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$instanceConsumer.CommandLineTemplate="-file C:\Windows\System32\Switch_LAN_TEST.ps1"
$result = $instanceConsumer.Put()
$newConsumer = $result.Path


#Bind filter and consumer
$instanceBinding = ([wmiclass]"\\.\root\subscription:__FilterToConsumerBinding").CreateInstance()
$instanceBinding.Filter = $newFilter
$instanceBinding.Consumer = $newConsumer
$result = $instanceBinding.Put()
$newBinding = $result.Path


##Removing WMI Subscriptions using [wmi] and Delete() Method
#([wmi]$newFilter).Delete()
#([wmi]$newConsumer).Delete()
#([wmi]$newBinding).Delete()
我可以关闭powershell会话,脚本将启动,但如果我重新启动计算机,永久事件将再次存在,但脚本无法工作

有什么想法吗?
谢谢。

我在Windows 10上检查了我的脚本,即使在重新启动后也可以正常工作。“不工作”是什么意思?显示输出、期望等。永久事件不会调用脚本。我没有输出或错误。可能Windows 7需要其他东西。我刚刚找到了重新启动后无法工作的原因,这一点非常重要creatorSID是本地管理员。请看:我在Windows 10上检查了脚本,即使重新启动后也能正常工作。“不工作”是什么意思?显示输出、期望等。永久事件不会调用脚本。我没有输出或错误。可能Windows 7需要其他东西。我刚刚找到了重新启动后无法工作的原因。对于我来说,CreatorID是本地管理员非常重要。请参见: