2011-04-12 56 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

相關問題