【问题标题】:How to terminate processes from a wmi permanent event consumer如何从 wmi 永久事件使用者终止进程
【发布时间】: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)

【问题讨论】:

    标签: vbscript wmi


    【解决方案1】:

    我不知道这是什么原因,但我也从未设法让它工作。乍一看应该——TargetEvent.TargetInstance.Name 返回进程名等。但是在调用方法时,wbemess.log 中写入错误:

    脚本引擎说:Microsoft VBScript 运行时错误:对象不支持此属性或方法:'TargetEvent.TargetInstance.Terminate' (Wed Apr 13 19:44:54 2011.15735734):在命名空间 //./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;
    };
    

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

    您未能使用 WshShell 对象,因为您尝试使用 WScript.CreateObject 创建它,而 ActiveScriptConsumer VBScript 引擎无法使用 WScript。如果您改用 VBScript CreateObject() 函数,它应该可以工作。与 WshNetwork 相同。

    【讨论】:

    • 非常感谢。我将在今天晚些时候对此进行测试。至于我想这样做的原因。临时解决在我的系统上创建的一堆 rundll32.exe 进程,直到我们从供应商那里得到修复并且还想学习如何使用永久事件消费者。
    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多