如何获取远程机器上正在运行的进程描述?

8
我已经尝试了两种方法来完成这个任务。
第一种方法,我使用了 System.Diagnostics,但是在 MainModule 上出现了一个 "Feature is not supported for remote machines" 的 NotSupportedException 错误。
foreach (Process runningProcess in Process.GetProcesses(server.Name))
{
    Console.WriteLine(runningProcess.MainModule.FileVersionInfo.FileDescription);
}

我尝试了第二种方法,使用了 System.Management,但似乎 ManagementObjectDescriptionName 相同。
string scope = @"\\" + server.Name + @"\root\cimv2";
string query = "select * from Win32_Process";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject obj in collection)
{
    Console.WriteLine(obj["Name"].ToString());
    Console.WriteLine(obj["Description"].ToString());
}

有没有更好的方法可以获取远程计算机上正在运行进程的描述信息?


你尝试过使用Rob van der Woude的wmigen吗?它可能有助于展示可用的内容。http://www.robvanderwoude.com/wmigen.php - Lizz
@Lizz 嗯,我已经尝试循环遍历 obj 的属性,并检查 Property.ToString() 是否包含应该在我正在搜索的进程描述中的关键字... - athom
哎呀,抱歉,我想不出其他的了。:( 这很有趣 - 也很奇怪。+1 鼓励你写出好的代码并解决问题! :) - Lizz
1
https://dev59.com/nXM_5IYBdhLWcg3w3nRs - John Bartels
1个回答

5

我认为我有一种方法可以满足我的需求。基本上,我从ManagementObject中获取文件路径,并从实际文件中获取描述。

ConnectionOptions connection = new ConnectionOptions();
connection.Username = "username";
connection.Password = "password";
connection.Authority = "ntlmdomain:DOMAIN";

ManagementScope scope = new ManagementScope(@"\\" + serverName + @"\root\cimv2", connection);
scope.Connect();

ObjectQuery query = new ObjectQuery("select * from Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject obj in collection)
{
    if (obj["ExecutablePath"] != null)
    {
        string processPath = obj["ExecutablePath"].ToString().Replace(":", "$");
        processPath = @"\\" + serverName + @"\" + processPath;

        FileVersionInfo info = FileVersionInfo.GetVersionInfo(processPath);
        string processDesc = info.FileDescription;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接