2
votes

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.

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
In our environment there are restrictions to certain machines, and only certain service Accounts can be used to access these - Johan Aspeling

1 Answers

4
votes

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.