远程机器上是否正在运行一个进程?

7

我有三台远程电脑需要进行远程连接。我正在尝试编写一个简单的Windows应用程序,它将在一个窗口中显示特定进程是否在任何一台机器上运行,例如:

服务器1:Chrome未运行

服务器2:Chrome正在运行

服务器3:Chrome正在运行

我使用了WMI和C#。到目前为止,我已经做到了这么多:

            ConnectionOptions connectoptions = new ConnectionOptions();

            connectoptions.Username = @"domain\username";
            connectoptions.Password = "password";

            //IP Address of the remote machine
            string ipAddress = "192.168.0.217";
            ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2");
            scope.Options = connectoptions;
            //Define the WMI query to be executed on the remote machine
            SelectQuery query = new SelectQuery("select * from Win32_Process");

            using (ManagementObjectSearcher searcher = new
                        ManagementObjectSearcher(scope, query))
            {

                ManagementObjectCollection collection = searcher.Get();
                foreach (ManagementObject process in collection)
                {
                    // dwarfs stole the code!! :'(                        
                }
            }

我认为一切设置都正确,但如果我在foreach循环中使用MessageBox.Show(process.ToString()),我会得到一堆包含以下文本的消息框:

\\username\root\cimv2:W32_Process.Handle="XXX"

我有些困惑。有没有办法将XXX“翻译”成进程名称?或者,我该如何获取进程的名称,以便使用if语句检查它是否是“chrome”进程?
或者...我的实现是否过于复杂?有更简单的方法来完成这个任务吗?
非常感谢!
3个回答

7
在您的foreach中,尝试使用以下代码:
Console.WriteLine(process["Name"]);

1
我在哪里可以找到像“Name”这样的属性列表?它能用,只是不确定你从哪里得到的。 - Krzysiek
好问题 - 必须有一个列表。如果我没记错的话,我最初是从CodeProject.com上的一个示例中得到的。 - 500 - Internal Server Error
3
Win32_Process WMI类的属性列在MSDN文档中,链接为http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372%28v=vs.85%29.aspx。 - RRUZ
@RRUZ,我之前就在那个网站上!我只是没有滚到最底部:( 谢谢!已经加入书签了!:) - Krzysiek
你也可以使用类似wmi-delphi-code-creator这样的工具来检查WMI类并生成代码。 - RRUZ

3
您可以在WQL语句中过滤要监视的进程名称,因此您可以编写类似于以下内容的内容。
 SelectQuery query = new SelectQuery("select * from Win32_Process Where Name='Chrome.exe'");

尝试这个示例应用程序。
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";
                    Conn.Password  = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='Chrome.exe'");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);


                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                  //for each instance found, do something  
                  Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

2
尝试使用Process.GetProcesses("chrome", "computerName");。
定义在System.Diagnostics.Process中。
public static Process[] GetProcessesByName(
   string processName,
   string machineName)

这可能是一个新手问题,但我该如何指定远程计算机的计算机名称?也就是说,在哪里指定IP、用户名、密码等信息? - Krzysiek
你不知道要连接的机器名称吗?你需要知道名称和/或IP地址。您还需要能够从监控计算机作为管理员登录到被监视的计算机。 - agent-j
2
我不能只说(“chrome”,“ipnumber”)…那么我在哪里提供用户名和密码? - Krzysiek
+1 是最容易编写和阅读的代码,尤其是如果您在帐户下运行它已经拥有权限。如果当前帐户没有权限(或者在服务器端代码中遇到“NTLM单跳”问题),则在调用此方法之前需要模拟另一个帐户。 - Alexei Levenkov

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