WMI管理范围连接到本地而不是遥控器



我试图连接到远程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)

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

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(@"\remoteMachineHostnamerootcimv2", options);  

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

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

to:

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

另一个选项:

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

请参阅此链接(这是Microsoft示例)

编辑

如果要与deffrent用户连接,则需要传递连接(请参见上文链接)

最新更新