2012-06-01 4 views
7

원격으로 연결할 수있는 원격 PC가 3 대 있습니다. 하나의 창에서 특정 프로세스가 둘 중 하나의 머신에서 실행 중인지 여부를 표시하는 간단한 Windows 응용 프로그램을 작성하려고합니다.프로세스가 원격 시스템에서 실행되고 있습니까?

서버 1 : 크롬 서버 2

를 실행하지 않는 크롬이 실행되는

Server3을 : 크롬

I는 WMI와 C# 사용

실행된다. 지금까지 나는이 정도 가지고 :

  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!! :'(      
       } 
      } 

내가 그것을 모두 올바르게 설정되어 생각하지만, 내가 MessageBox.Show 경우 (process.ToString())를 foreach 루프 안에, 내가 메시지의 전체 무리를 얻을 수 다음 텍스트가 포함 된 상자 :

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

나는 걸려 있습니다. XXX을 프로세스 이름으로 "변환"할 수있는 방법이 있습니까? 아니면, 어떻게 실제로 "크롬"프로세스인지 여부를 확인하기 위해 if 문을 사용할 수 있도록 프로세스 이름을 얻을 수 있습니까?

또는 ... 과장된 구현입니까? 이 작업을 수행하는 더 쉬운 방법이 있습니까?

고맙습니다.

답변

6

,이 시도 :

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

"이름"과 같은 속성 목록을 어디에서 찾을 수 있습니까? 그것은 작동합니다. 어디에서 가져 왔는지 확실하지 않습니다. – Krzysiek

+0

좋은 질문 - 어딘가에 목록이 있어야합니다. IIRC, 나는 원래 CodeProject.com의 예제에서 이것을 얻었다. –

+3

Win32_Process WMI 클래스의 속성은 MSDN 설명서에 나와 있습니다. http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372%28v=vs.85%29.aspx – RRUZ

2

시도 Process.GetProcesses("chrome", "computerName");

당신은 WQL 문장에서 볼 수있는 프로세스의 이름을 필터링 할 수 있습니다

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

아마도 초보자 용 질문 일 테지만 원격 컴퓨터의 computerName은 어떻게 지정합니까? 즉, IP, 사용자 이름, 암호를 지정하는 위치는 어디입니까? – Krzysiek

+0

연결하려는 시스템의 이름을 모를 수 있습니까? 이름 및/또는 IP 주소를 알아야합니다. 또한 모니터링 컴퓨터에서 관리자로 모니터링되는 컴퓨터에 로그인 할 수 있어야합니다. –

+1

나는 ("chrome", "ipnumber")라고 말할 수 없다. 사용자 이름/비밀번호는 어디에서 제공하나요? – Krzysiek

3

로 System.Diagnostics.Process가에 정의, 그래서 당신은이

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

같은 것을 쓸 수있는이 시도 당신의 foreach에서 샘플 응용 프로그램

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(); 
     } 
    } 
} 
관련 문제