简体   繁体   中英

how to get all the users processes in WMI

i want to get all the user's process as stated in the task manager process's (system, administrator, network service and local service).

i need to get it through the WMI, i couldn't find the username of each process and i have checked the wmi process and task manager process, the wmi shows only the administrator process alone.

can you help me to get the all user's process list???

you can browse Win32_Process class to get process details:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_Process"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_Process instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

or in the c#, without using WMI :

Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist)
  {
    Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
  }

Maybe it is a bit late but I think it is possible using WMI through GetOwner() method of the Win32_Process class which retrieves the user name and domain (Below code is not mine, I have extracted it from http://social.msdn.microsoft.com/Forums/en-US/d842c407-18f5-478b-8c4f-7e14ac4fbbe6/get-owner-of-curently-runing-procesess ):

using System;
using System.Diagnostics;
using System.Management;   // Add reference to System.Management!!

class Program {
  static void Main(string[] args) {
    ManagementObjectSearcher searcher =
         new ManagementObjectSearcher("root\\CIMV2",
         "SELECT * FROM Win32_Process");

    foreach (ManagementObject queryObj in searcher.Get()) {
      ManagementBaseObject outParams =
         queryObj.InvokeMethod("GetOwner", null, null);
      Console.WriteLine("{0} owned by {1}\\{2}", queryObj["Name"],
        outParams["Domain"], outParams["User"]);
    }
    Console.ReadLine();
  }
}

Also If you are interested in you can do it with vbscript using below code to determine the account name under which a process is running (see below page for more detailed info http://msdn.microsoft.com/en-us/library/aa394599(v=vs.85).aspx ):

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process")
For Each objProcess in colProcessList
    colProperties = objProcess.GetOwner( _
        strNameOfUser,strUserDomain)
    Wscript.Echo "Process " & objProcess.Name _
        & " is owned by " _ 
        & strUserDomain & "\" & strNameOfUser & "."
Next

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM