WMI 管理范围连接到本地而不是远程

WMI management scope connect to local instead of remote

我尝试连接到远程 PC 并查询其进程,但是当我 运行 代码时,它连接到我的本地 PC 并获取其进程而不是远程 PC。 代码是

ManagementScope scope = new ManagementScope(@"\remote-user\root\cimv2");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)

(我假设远程用户是完整的计算机名称)更改:

ManagementScope scope = new ManagementScope(@"\remote-user\root\cimv2");

至:

ManagementScope scope = new ManagementScope(@"\<FullComputerName>\root\cimv2");

另一个选项:

ManagementScope scope = new ManagementScope("\\<FullComputerName>\root\cimv2");

参见 this link(这是 Microsoft 示例)

编辑

如果您想连接不同的用户,您需要传递 ConnectionOptions(见上文 link)

您似乎将远程计算机的用户名 ("remote-user") 而不是主机名传递给您的管理范围。将您的代码更改为例如:

ConnectionOptions options = new ConnectionOptions();
options.Password = "remoteUserPassword"; // you may want to avoid plain text password and use SecurePassword property instead
options.Username = "remote-user"; 
ManagementScope scope = new ManagementScope(@"\remoteMachineHostname\root\cimv2", options);