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

送分小仙女□ 提交于 2019-11-30 09:05:36
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.

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

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

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

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

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.

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

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

https://docs.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!