views:

3745

answers:

3

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.

+2  A: 

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(); }
hipplar
This is the exact same code I use for this. I have run into the "Access Denied" message when trying to connect to Windows 2008 Server.
hectorsosajr
When I run the above code, I get an message stating: Cannot open notepad service on computer ComputerName. I am trying to check the process and this seems to be checking a service. Can it do both?
I was not thinking about this not being a service... I am now not sure that I gave you a good answer.
hipplar
I just tested my code with notpad on a remote machine and got the same message you got. I ran your code without errors. How are you running your code? Do you have access to the remote machine. I know I am a local admin on the remote machine I tested with.
hipplar
Below is what I did, and it is working. Thanks for your help.
A: 

Does the inner Exception say "Access Denied"?

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

http://stackoverflow.com/questions/152337/getprocessesbyname-and-windows-server-2003-scheduled-task

Chris Persichetti
A: 

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