Terminating a process using WMI

Hi,

I have been told that this is possible but I can't seem to find any sample code anywhere. Anyone know how to do this

Thanks.



Answer this question

Terminating a process using WMI

  • kangalert

    you can do it with both without problems. The only problem being is permission to kill a process on the remote computer. you need to have permission to do this and access rights

  • Lawrence 007

    except your posting how to do it locally and sven is proposing a remote/local function.
  • Shabari

    no worries ;-)

  • _Gerry_

    re-read the post. look at the last example as well as svens

  • Ibrahim Hamouda

    pretty much how Sven posted, either doing it this way:

    Process[] theList = Process.GetProcesses("machineName")

    foreach(Process curProcess in theList)

    {

       if (curProcess.ProcessName.Equals("ProcessName"))

       {

          curProcess.Kill();

       }

    }

     

    or this way:

     

    Process[] theList = Process.GetProcessesByName("machineName", "ProcessName")

    foreach(Process curProcess in theList)

    {

       curProcess.Kill();

    }



  • brian_tsim

    using WMI, be sure you added a reference to System.Management.dll and imported the namespaces, System.Management and System.Management.Instrumentation.

    we then execute a query and execute the terminate command:

    ManagementScope theScope = new ManagementScope("\\\\ComputerName\\root\\cimv2");

    ObjectQuery theQuery = new ObjectQuery("SELECT * FROM Win32_Process WHERE Name='ApplicationName.exe'");

    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);

    ManagementObjectCollection theCollection = theSearcher.Get();

    foreach (ManagementObject theCurObject in theCollection)

    {

    theCurObject.InvokeMethod("Terminate", null);

    }



  • AndyJ_PS

    Thanks, your WMI sample is perfect! However, just out of interest, how can I do this using the Process class

    Thanks.


  • Nisa

    The problem that I am having is: How do I kill the process . I heard that it is possible with WMI but not with the Process class. Is this the case

    Thanks


  • Cole Thompson

    I need to be able to end remote processes as well, the process class is no good for that.


  • markgoldin

    you are aware that you can use the Process class to terminate a process right It would be cheaper also than using WMI, if its possible in WMI

  • Ray Laubert

    Oh damn ok sorry I haven't seen that you were getting the processes by machinename ok then i guess yours as well.
  • Norbert.Bender

    It is:

    Process.GetProcesses("remoteMachine") will give you a process collection of remoteMachine.

    Process.GetProcessByName("notepad", "remoteMachine") will give you a collection of processes named notepad on remoteMachine.

    Given that you have access rights on remoteMachine...

    --
    SvenC


  • Terminating a process using WMI