如何获得WMI的所有用户进程(how to get all the users processes

2019-08-17 08:36发布

我希望得到所有用户的进程在任务管理器进程的(系统管理员,网络服务和本地服务)说。

我需要通过WMI得到它,我无法找到每个进程的用户名和我检查了WMI进程和任务管理器的过程中,WMI只显示单独的管理员进程。

你能帮助我得到所有用户的进程列表???

Answer 1:

你可以浏览Win32_Process类获取进程的详细信息:

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

或在c#,而无需使用WMI

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


Answer 2:

也许这是一个有点晚了,但我认为这是可能使用WMI通过GetOwner以检索用户名和域(下面的代码是不是我的Win32_Process类的()方法,我已经从提取它的http://social.msdn。 microsoft.com/Forums/en-US/d842c407-18f5-478b-8c4f-7e14ac4f​​bbe6/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();
  }
}

此外,如果你有兴趣,你可以使用下面的代码来确定帐户名称的进程在其下运行使用VBScript做(见后页更详细的信息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

希望能帮助到你!



文章来源: how to get all the users processes in WMI
标签: process wmi