How can I check if a specific process is running on a remote PC/Server?

好久不见. 提交于 2021-02-10 22:24:55

问题


string ComputerName = serverName;
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(); // CRASH HERE
ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='" + processName + "'");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

The message that shows up is:

Value does not fall within the expected range.


回答1:


This is most likely to do with wrong credentials or insufficient permissions. In your case no username provided - I am fairly sure you cannot pass empty username. The username/password you use for WMI query have to exist on the remote PC (plus the user has to have sufficient permissions).

If you want to use the same username/password you are logged in with on the local PC (from which you are running the code), you should omit the whole ConnectionOptions part:

    //ConnectionOptions Conn = new ConnectionOptions();
    //Conn.Username = "";
    //Conn.Password = "";
    //Conn.Authority = "ntlmdomain:DOMAIN";

I tried your code (added last 4 lines for testing) and it came up with the same error as you have. Once I added username and password everything is running fine.

string ComputerName = "10.1.2.3";
ManagementScope Scope;

if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
{
    ConnectionOptions Conn = new ConnectionOptions();
    Conn.Username = "Administrator";
    Conn.Password = "pass123";
    //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(); // CRASH HERE
ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='" + "cmd.exe" + "'");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

ManagementObjectCollection queryCollection = Searcher.Get();

foreach (var item in queryCollection)
    Console.WriteLine(item["Description"]);

Console.Read();

I also tried the same code with the section regarding ConnectionOptions commented out and it also works. Note, however, that according to what I wrote earlier, I had to create a user on the remote PC, which has the same credentials as the user I am logged in on the local PC.

Hope this helps.

EDIT: Also as per Maximilian Gerhardt comment, skip NULL this line:

Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);


来源:https://stackoverflow.com/questions/34271920/how-can-i-check-if-a-specific-process-is-running-on-a-remote-pc-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!