杀死远程机器上的进程

我正试图杀死远程机器上的进程。 但我得到错误。 我做错了什么,我该怎么做才能做到这一点?

我的代码:

var iu = new ImpersonateUser(); try { iu.Impersonate(Domain, _userName, _pass); foreach (var process in Process.GetProcessesByName("notepad", "RemoteMachine")) { string processPath = pathToExe; //Is set as constant (and is correct) process.Kill(); Thread.Sleep(3000); Process.Start(processPath); } } catch (Exception ex) { lblStatus.Text = ex.ToString(); } finally { iu.Undo(); } 

只是为了澄清ImpersonateUser,它让我以正确的用户权限登录到远程机器。 所以问题不在那里。 当我调试并检查过程对象时,在这种情况下我找到了记事本的正确进程ID。 所以连接工作正常。 但是当我尝试杀死进程时,我收到此错误:

 System.NotSupportedException: Feature is not supported for remote machines. at System.Diagnostics.Process.EnsureState 

System.Diagnostics.Process类无法终止远程进程。 您可以使用System.Management命名空间(确保设置引用),以使用WMI。

一个简单的例子如下。

 var processName = "iexplore.exe"; var connectoptions = new ConnectionOptions(); connectoptions.Username = @"YourDomainName\UserName"; connectoptions.Password = "User Password"; string ipAddress = "192.168.206.53"; ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2", connectoptions); // WMI query var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'"); using (var searcher = new ManagementObjectSearcher(scope, query)) { foreach (ManagementObject process in searcher.Get()) // this is the fixed line { process.InvokeMethod("Terminate", null); } } Console.ReadLine();