【发布时间】:2011-04-12 14:31:52
【问题描述】:
我正在尝试创建一个永久的 wmi 事件使用者,它将等待使用特定命令行参数创建进程然后终止它。
到目前为止,我可以让我的事件处理程序在预期时触发并写入测试日志文件。 我什至可以使用 TargetEvent.TargetInstance 从 WMI 事件中访问参数。但是,当我尝试对其调用终止时,它会失败。
我也无法创建像 wscript.shell 或 wscript.network 这样无法创建实例的对象实例。我相信这可能是因为这个脚本实际上并没有在 windows 脚本主机中运行。
所以我的问题是如何让终止方法在我的 Win32_Process 实例上工作,或者有没有办法调用外部命令(假设我不能使用 wscript.shell 对象)。
我从这里获得了有关如何创建 mof 文件的大部分详细信息: http://www.codeproject.com/KB/system/PermEvtSubscriptionMOF.aspx?display=Print
我的安装 Mof 文件如下:
#pragma namespace("\\\\.\\root\\subscription")
instance of __EventFilter as $EventFilter
{
Name = "My Test Filter";
EventNamespace = "Root\\Cimv2";
Query = "Select * From __InstanceCreationEvent Within 2 "
"Where TargetInstance Isa \"Win32_Process\" "
"And Targetinstance.Name = \"notepad.exe\" "
"And Targetinstance.CommandLine LIKE \"%test.txt%\"";
QueryLanguage = "WQL";
};
instance of ActiveScriptEventConsumer as $Consumer
{
Name = "MyTestConsumer";
ScriptingEngine = "VBScript";
ScriptText =
"On Error Resume Next\n"
"'Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
"Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
"Set objFile = objFSO.OpenTextFile(\"c:\\log.txt\", 8, True)\n"
"objFile.WriteLine Time & \" \" & \" notepad started \" & TargetEvent.TargetInstance.Handle \n"
"objFile.Close\n"
"TargetEvent.TargetInstance.Terminate()\n";
};
instance of __FilterToConsumerBinding
{
Filter = $EventFilter;
Consumer = $Consumer;
};
我的删除 mof 文件是:
#pragma namespace("\\\\.\\root\\subscription")
#Pragma deleteInstance("__EventFilter.Name=\"My Test Filter\"",FAIL)
#Pragma deleteInstance("ActiveScriptEventConsumer.Name=\"MyTestConsumer\"",FAIL)
#pragma deleteinstance ("__FilterToConsumerBinding.Consumer="
"\"\\\\\\\\.\\\\root\\\\subscription:ActiveScriptEventConsumer.Name=\\\"MyTestConsumer\\\"\","
"Filter=\"\\\\\\\\.\\\\root\\\\subscription:__EventFilter.Name=\\\"My Test Filter\\\"\"", FAIL)
【问题讨论】: