Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4323984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:04:20+00:00 2026-05-21T09:04:20+00:00

I am trying to create a permanent wmi event consumer that will wait for

  • 0

I am trying to create a permanent wmi event consumer that will wait for a process to be created with a specific commandline parameter then terminate it.

So far I can get my event handler to fire when expected and write to a test log file.
I can even access parameters from the WMI event by using the TargetEvent.TargetInstance. However when i try to call terminate on it, it fails.

I am also having trouble creating instances of objects like wscript.shell or wscript.network which fail to create an instance. I believe this might be because this script is not actually running in the windows script host.

So my question is how can I get the terminate method to work on my instance of Win32_Process or is there a way to call an external command (given I can’t use wscript.shell object).

I got most of the details on how to create my mof file from here:
http://www.codeproject.com/KB/system/PermEvtSubscriptionMOF.aspx?display=Print

My Setup Mof File is the following:

#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;
};

My removal mof file is:

#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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:04:20+00:00Added an answer on May 21, 2026 at 9:04 am

    I have no idea what is the reason for this, but I have never managed to make it work either. At first glance it should – TargetEvent.TargetInstance.Name returns the process name, etc. But when calling a method, an error is written to wbemess.log:

    Scripting engine says: Microsoft VBScript runtime error: Object doesn’t support this property or method: ‘TargetEvent.TargetInstance.Terminate’
    (Wed Apr 13 19:44:54 2011.15735734) : Dropping event destined for event consumer ActiveScriptEventConsumer=”TestConsumer” in namespace //./root/subscription

    Here is my workaround:

    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;
    };
    

    In the script I use SWbemServices.Get() to get the created process instance and then Terminate works. Just pass TargetEvent.TargetInstance.Handle to SWbemServices.Get().

    You failed to use WshShell object because you attempted to create it with WScript.CreateObject and WScript is not available to the ActiveScriptConsumer VBScript engine. It should work if you use the VBScript CreateObject() function instead. Same with WshNetwork.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a user account in a test. But getting a Object reference
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
Trying to create a list to return some JSON data to a view. Following
I'm trying to create a bookmarklet for posting del.icio.us bookmarks to a separate account.
I'm trying to create a sitemap using Linq to Xml, but am getting an
I'm trying to create a webapplication where I want to be able to plug-in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.