如何检查特定进程是否在远程 PC/服务器上运行? [英] How can I check if a specific process is running on a remote PC/Server?

查看:30
本文介绍了如何检查特定进程是否在远程 PC/服务器上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

显示的消息是:

值不在预期范围内.

推荐答案

这很可能是由于凭据错误或权限不足造成的.在你的情况下没有提供用户名 - 我很确定你不能传递空用户名.您用于 WMI 查询的用户名/密码必须存在于远程 PC 上(而且用户必须具有足够的权限).

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).

如果您想使用在本地 PC 上登录时使用的相同用户名/密码(您正在运行代码),您应该省略整个 ConnectionOptions 部分:

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";

我尝试了您的代码(添加了最后 4 行用于测试),但出现了与您相同的错误.添加用户名和密码后,一切正常.

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

我也尝试了相同的代码,其中关于 ConnectionOptions 的部分被注释掉了,它也有效.但是请注意,根据我之前写的内容,我必须在远程 PC 上创建一个用户,该用户与我在本地 PC 上登录的用户具有相同的凭据.

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.

希望这会有所帮助.

同样根据 Maximilian Gerhardt 的评论,跳过 NULL 这一行:

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

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

这篇关于如何检查特定进程是否在远程 PC/服务器上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆