2016-04-27 63 views
1

我无法连接到远程计算机以获取正在运行的进程列表。 对于我的测试机器,我使用用户名@“ownme \ veritas”。密码只是“veritas”。 示例域是“ownme”。拒绝访问.Net GetProcesses

return new System.Management.ConnectionOptions() 
{ 
    //certainly these variables have been checked and are correct 
    Username = UserCredential.DomainUser, 
    Password = UserCredential.Password 
}; 

这就是我试图做的连接。我不知道,但这可能实际上是这里的问题。这也可能是因为我没有在上面的ConnectionOptions中填写足够的字段。 我提到这两篇文章:

https://www.experts-exchange.com/questions/23514935/How-to-use-GetProcess-for-remote-sytems.html

https://msdn.microsoft.com/en-us/library/system.management.connectionoptions.authentication.aspx

我想不出我做错了什么

ManagementScope scope = new ManagementScope($"\\\\{computer.DnsHostname}\\root\\cimv2", connectionOptions); 
scope.Connect(); 

//Error: Access is denied 
var processes = System.Diagnostics.Process.GetProcesses(dnsHostName); 
+0

您没有使用WMI为您GetProcesses查询 –

+0

@YacoubMassad,我想使用WMI的基础过程。 – Bluebaron

回答

0

GetProcesses将使用当前用户凭据连接到远程机器,而不是您通过ConnectionOptions指定的凭据。

您需要使用您用正确的凭据创建的WMI范围对象发出了这样的过程的查询:

//.. 

SelectQuery query = new SelectQuery("select * from Win32_Process"); //query processes 

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) 
{ 
    using (ManagementObjectCollection collection = searcher.Get()) 
    { 
     foreach (var process in collection) //loop through results 
     { 
      var name = process["name"]; //process name 
      //Do something with process 
     } 
    } 
} 
+0

如果我冒充用户该怎么办? – Bluebaron

+0

模拟工作。谢谢。 – Bluebaron