Is the process running on a remote machine?

I have three remote computers that I connect to remotely. I am trying to write a simple Windows application that will appear in a single window regardless of whether any particular process is running on any of the computers, for example.

Server1: Chrome is not working

Server2: running Chrome IS

Server3: running Chrome IS

I used WMI and C #. So far I have a lot:

ConnectionOptions connectoptions = new ConnectionOptions(); connectoptions.Username = @"domain\username"; connectoptions.Password = "password"; //IP Address of the remote machine string ipAddress = "192.168.0.217"; ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2"); scope.Options = connectoptions; //Define the WMI query to be executed on the remote machine SelectQuery query = new SelectQuery("select * from Win32_Process"); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) { ManagementObjectCollection collection = searcher.Get(); foreach (ManagementObject process in collection) { // dwarfs stole the code!! :'( } } 

I think everything is set up correctly, but if I MessageBox.Show (process.ToString ()) inside the foreach loop, I get a whole bunch of message boxes with the following text:

 \\username\root\cimv2:W32_Process.Handle="XXX" 

I'm kinda stuck. Is there a way to "translate" XXX into the process name? Or else, how can I get the names of the processes so that I can use the if statement to check if this is a chrome process?

Or ... is my implementation redundant? Is there an easier way to do this?

Thank you so much!

+7
source share
3 answers

In your foreach, try the following:

 Console.WriteLine(process["Name"]); 
+6
source

You can filter the process name for viewing in the WQL clause so you can write something like this

  SelectQuery query = new SelectQuery("select * from Win32_Process Where Name='Chrome.exe'"); 

Try this sample application.

 using System; using System.Collections.Generic; using System.Management; using System.Text; namespace GetWMI_Info { class Program { static void Main(string[] args) { try { string ComputerName = "localhost"; ManagementScope Scope; if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) { ConnectionOptions Conn = new ConnectionOptions(); Conn.Username = ""; Conn.Password = ""; Conn.Authority = "ntlmdomain:DOMAIN"; Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn); } else Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null); Scope.Connect(); ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='Chrome.exe'"); ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query); foreach (ManagementObject WmiObject in Searcher.Get()) { //for each instance found, do something Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]); } } catch (Exception e) { Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace)); } Console.WriteLine("Press Enter to exit"); Console.Read(); } } } 
+3
source

Try Process.GetProcesses("chrome", "computerName") ;

Defined in System.Diagnostics.Process as

 public static Process[] GetProcessesByName( string processName, string machineName) 
+2
source

All Articles