2

I wrote a monitoring service that keeps track of a set of processes, and notifies when the service is acting strange, running on high memory usage, exceeding CPU runtime etc.

This is working fine one my local computer, but I need it to point to remote machines and get the Process Information on these machines.

My approach, working on Local:

Process.GetProcesses().ToList().ForEach(p => MyProcesses.Add(p));

This gives me a collection of Processes on my machine, there is a lot more logic to it (filtering processes by name etc), but this is what it comes down to.

What I need:

Process.GetProcesses(machineName).ToList().ForEach(p => MyProcesses.Add(p));

This obviously complains about Access, the question is how can I pass credentials to somehow use this functionality?

Note: I have all of the machineNames and credentials needed to access these processes, but I need to supply this in a config file. I can run the Service in the specified username, but it still complains about access.

Johan Aspeling
  • 765
  • 1
  • 13
  • 38
  • 1
    Why use separate accounts instead of creating one domain account for the service that has permissions to access all machines? This is infinitely safer than storing usernames and accounts – Panagiotis Kanavos Jun 01 '15 at 09:35
  • In our environment there are restrictions to certain machines, and only certain service Accounts can be used to access these – Johan Aspeling Jun 01 '15 at 12:58

1 Answers1

4

You need the GetProcesses(string) overload that accepts a target machine name, eg:

Process[] remoteAll = Process.GetProcesses("myComputer");

From the documentation example, you will see that the GetProcessBy methods also have overloads that accept a target name.

Under the hood, the Process class uses WMI to access management information like the list of processes.

For advanced scenarios it is probably better and faster to create a WMI query that will return the data you need, just like a database, instead of enumerating the remote process and extracting the data from each one.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • The thing is I like the way the Process class simplifies things. But I think the only way I will achieve this is through WMI. I will quickly look into this, thanks – Johan Aspeling Jun 01 '15 at 09:26
  • @JohanAspeling why? WMI isn't going to bypass security restrictions. If the machines are in the same domain, you simply need to give the proper permissions to the account that will run the code. This is also the safest approach. Do you want to access machines that are not in a domain, or do you need to impersonate some other domain account? – Panagiotis Kanavos Jun 01 '15 at 09:32
  • The machines are all in the same domain, from your comment I assume I should give the user that is running the service, access to the computer that contains the processes I want to monitor. Where will I give this user that access? – Johan Aspeling Jun 01 '15 at 13:01