Check if a process is running on a remote system u

2019-02-09 11:50发布

问题:

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.

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.

回答1:

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.

ServiceController sc = new ServiceController();
sc.MachineName = remoteSystem;
sc.ServiceName = procSearc;

if (sc.Status.Equals(ServiceControllerStatus.Running))
{
   sc.Stop();
}
else
{
   sc.Start();
}


回答2:

Below is what I did to get this to work:

First I added a reference to System.ServiceProcess and added: 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);
   }


回答3:

Does the inner Exception say "Access Denied"?

A similar question may help, it mentions needing to be in the Performance Monitor Users group.

GetProcessesByName() and Windows Server 2003 scheduled task



回答4:

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.

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.



回答5:

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

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

 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);
            });
    }