Chris
Chris

Reputation:

Check if a process is running on a remote system using C#

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.

Upvotes: 9

Views: 40054

Answers (6)

Xavier
Xavier

Reputation: 83

You may try impersonate to the user that have access to the remote server.

https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsimpersonationcontext?redirectedfrom=MSDN&view=netframework-4.7.2

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.

LogonUser works only for my domain

Upvotes: 0

Nick Prozee
Nick Prozee

Reputation: 2913

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

Upvotes: 0

Larry Hipp
Larry Hipp

Reputation: 6255

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

Upvotes: 8

Marcell
Marcell

Reputation: 949

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.

Upvotes: 0

Crispy
Crispy

Reputation: 5637

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

Upvotes: 1

Chris
Chris

Reputation:

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

Upvotes: 4

Related Questions