2011-04-12 55 views
1

我正在尝试创建一个永久wmi事件使用者,它将等待使用特定命令行参数创建进程,然后终止它。如何终止来自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) 

回答

1

我不知道这是什么原因,但我从来没有设法使其工作。乍一看它应该--TargetEvent.TargetInstance.Name返回进程名称等,但是当调用一个方法时,错误被写入到wbemess.log中:

脚本引擎说:Microsoft VBScript运行时错误:对象没有支持此属性或方法:TargetEvent.TargetInstance.Terminate' (星期三年04月13 19点44分54秒2011.15735734):在命名空间删除往事件消费者ActiveScriptEventConsumer =“TestConsumer”事件//./root/subscription

这是我的解决方法:

instance of __EventFilter as $EventFilter 
{ 
    EventNamespace = "Root\\Cimv2"; 
    Name = "New Process Instance Filter"; 
    Query = "Select * From __InstanceCreationEvent Within 2" 
      "Where TargetInstance Isa \"Win32_Process\" " 
      "And Targetinstance.Name = \"notepad.exe\" "; 
    QueryLanguage = "WQL"; 
}; 

instance of ActiveScriptEventConsumer as $Consumer 
{ 
    Name = "TargetEventConsumer"; 
    ScriptingEngine = "VBScript"; 
    ScriptText = 
    "Set objWmi = GetObject(\"winmgmts:\")\n" 
    "\n" 
    "Set objProcess = objWmi.Get(\"Win32_Process.Handle='\" _\n" 
    " & TargetEvent.TargetInstance.Handle & \"'\")\n" 
    "\n" 
    "objProcess.Terminate\n"; 
}; 

instance of __FilterToConsumerBinding 
{ 
    Consumer = $Consumer; 
    Filter = $EventFilter; 
}; 

在脚本中我使用SWbemServ ices.Get()获取创建的流程实例,然后终止工作。只需将TargetEvent.TargetInstance.Handle传递给SWbemServices.Get()即可。

您未能使用WshShell对象,因为您试图使用WScript.CreateObject创建它,并且WScript对于ActiveScriptConsumer VBScript引擎不可用。如果使用VBScript的CreateObject()函数,它应该可以工作。与WshNetwork一样。

+0

谢谢你。我将在今天晚些时候对此进行测试。至于为什么我想这样做。暂时解决一系列正在我的系统上创建的rundll32.exe进程,直到我们从供应商处获得修复程序,并且还想学习如何使用永久事件使用者。 – Wil 2011-04-13 23:32:35

+0

完美谢谢:)。 – Wil 2011-04-23 05:38:06

相关问题