使用 C# 检查进程是否在远程系统上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/865412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a process is running on a remote system using C#
提问by
I am trying to check if a process is running on a remote system. I am using the following code:
我正在尝试检查进程是否正在远程系统上运行。我正在使用以下代码:
string procSearc = "notepad";
string remoteSystem = "remoteSystemName";
Process[] proce = System.Diagnostics.Process.GetProcessesByName(procSearch, remoteSystem);
However, when I try to run the code, I get the following error: "Couldn't connect to remote machine."
但是,当我尝试运行代码时,出现以下错误:“无法连接到远程机器。”
I am able to run pslist with the following command: C:>pslist \remoteSystemName So I know it is possible to get the information I need, but I need it in the code.
我可以使用以下命令运行 pslist: C:>pslist \remoteSystemName 所以我知道可以获取我需要的信息,但我需要在代码中使用它。
Another possibility would be to integrate pslist into C# and search the list to see if the process is there, but I have not found information on how to do this.
另一种可能性是将 pslist 集成到 C# 中并搜索列表以查看进程是否存在,但我还没有找到有关如何执行此操作的信息。
回答by Larry Hipp
Use the System.ServiceProcess.ServiceController
class for a service. You can use Status
to check if it's running and the Stop()
and Start()
to control it.
将该System.ServiceProcess.ServiceController
类用于服务。您可以使用Status
,以检查它是否运行,Stop()
并Start()
对其进行控制。
ServiceController sc = new ServiceController();
sc.MachineName = remoteSystem;
sc.ServiceName = procSearc;
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
}
else
{
sc.Start();
}
回答by Chris Persichetti
Does the inner Exception say "Access Denied"?
内部异常是否显示“拒绝访问”?
A similar question may help, it mentions needing to be in the Performance Monitor Users group.
一个类似的问题可能会有所帮助,它提到需要加入 Performance Monitor Users 组。
回答by Chris Persichetti
Below is what I did to get this to work:
以下是我为使其正常工作所做的工作:
First I added a reference to System.ServiceProcess and added: using System.ServiceProcess;
首先我添加了对 System.ServiceProcess 的引用并添加: using System.ServiceProcess;
string remoteSystem = "remoteSystemName";
string procSearch = "notepad";
Process[] proc = System.Diagnostics.Process.GetProcessesByName(procSearch, remoteSystem);
if (proc.Length > 0)
{
Console.WriteLine("Able to find: " + proc[0]);
}
else
{
Console.WriteLine("Unable to find: " + procSearch);
}
回答by ohgodnotanotherone
I figured when running the application by itself I would get an exception window, because exceptions were not handled within my code...
我想当我自己运行应用程序时,我会得到一个异常窗口,因为我的代码中没有处理异常......
There I saw the reason in the stacktrace: access denied. The reason was that the user running the program that was calling the .NET method to get the process list was not part of the "Performance Monitor Users" group of the remote machine.
我在堆栈跟踪中看到了原因:访问被拒绝。原因是运行调用 .NET 方法以获取进程列表的程序的用户不属于远程计算机的“性能监视器用户”组。
After that I got another exception saying that the performance monitoring service was not running on the remote machine. So I started the corresponding service at the remote computer and voila it worked!
之后,我收到另一个异常,说性能监控服务没有在远程机器上运行。所以我在远程计算机上启动了相应的服务,瞧它工作了!
This was using a Windows 7 client trying to get the process list of a Windows 2008 Server.
这是使用 Windows 7 客户端尝试获取 Windows 2008 Server 的进程列表。
回答by Nick Prozee
Killing the remote process
I found that the Process.Kill() method does not work when the Process.MachineName has been set, so here is a solution for killing the process remotely, hope it helps others.
The extension method to create the method: KillRemoteProcess
杀死远程进程
我发现Process.Kill()方法在设置了Process.MachineName的情况下不起作用,所以这里提供一个远程杀死进程的解决方案,希望对大家有所帮助。
创建方法的扩展方法:KillRemoteProcess
public static class ProcessExtensions
{
public static void KillRemoteProcess(this Process p, string user, string password)
{
new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "TaskKill.exe",
Arguments = string.Format("/pid {0} /s {1} /u {2} /p {3}", p.Id, p.MachineName, user, password),
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true
}
}.Start();
}
}
And ofcourse the method to find the processes and consume the KillRemoteProcess
当然还有找到进程并使用 KillRemoteProcess 的方法
public static void KillProcessesRemote()
{
string targetProcessName = "myProcess"; //Do not put 'process.exe' here just 'process'
string targetMachine = "remotMachine"; //Target machine
string username = "myUser"; //Username
string password = "myPassword"; //Password
Parallel.ForEach<Process>( //Kill all processes found
source: System.Diagnostics.Process.GetProcessesByName(targetProcessName, targetMachine),
body: process => {
process.KillRemoteProcess(username, password);
});
}
回答by Xavier
You may try impersonate to the user that have access to the remote server.
您可以尝试模拟有权访问远程服务器的用户。
After Impersonation, you will no longer hit the error. Also, you have to make sure there is trust between domain, else impersonation will not work.
模拟后,您将不再遇到错误。此外,您必须确保域之间存在信任,否则模拟将不起作用。