2012-12-18 25 views
8

私はこれまでこれを達成するための2つの方法を試みました。リモートマシン上で実行中のプロセスの説明を取得する方法は?

最初の方法では、System.Diagnosticsを使用しましたが、MainModuleの「機能はリモートマシンではサポートされていません」のNotSupportedExceptionになります。

foreach (Process runningProcess in Process.GetProcesses(server.Name)) 
{ 
    Console.WriteLine(runningProcess.MainModule.FileVersionInfo.FileDescription); 
} 

第二の方法は、私はSystem.Managementを使用して試みたが、ManagementObjectDescriptionNameと同じで、彼女のようです。

string scope = @"\\" + server.Name + @"\root\cimv2"; 
string query = "select * from Win32_Process"; 
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
ManagementObjectCollection collection = searcher.Get(); 

foreach (ManagementObject obj in collection) 
{ 
    Console.WriteLine(obj["Name"].ToString()); 
    Console.WriteLine(obj["Description"].ToString()); 
} 

リモートマシン上で実行中のプロセスの説明を取得するより良い方法について知っている人はいますか?

+0

Rob van der Woudeのwmigenを試しましたか?利用可能なものを示すのに役立つかもしれません。 http://www.robvanderwoude.com/wmigen.php – Lizz

+0

@Lizz私はすでにobjのプロパティをループし、Property.ToString()に記述されていたはずのキーワードが含まれているかどうかを調べました。私が探しているプロセスのひとつ... – athom

+0

Yikes。申し訳ありませんが、他のことは考えられません。 :(これは面白い - と奇妙です+1良いコードとトラブルシューティングのために!:) – Lizz

答えて

4

私は私の目的のために十分にうまくいくこれを行う方法があると思います。私は基本的にファイルパスをManagementObjectから外し、実際のファイルから説明を取得しています。

ConnectionOptions connection = new ConnectionOptions(); 
connection.Username = "username"; 
connection.Password = "password"; 
connection.Authority = "ntlmdomain:DOMAIN"; 

ManagementScope scope = new ManagementScope(@"\\" + serverName + @"\root\cimv2", connection); 
scope.Connect(); 

ObjectQuery query = new ObjectQuery("select * from Win32_Process"); 
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
ManagementObjectCollection collection = searcher.Get(); 

foreach (ManagementObject obj in collection) 
{ 
    if (obj["ExecutablePath"] != null) 
    { 
     string processPath = obj["ExecutablePath"].ToString().Replace(":", "$"); 
     processPath = @"\\" + serverName + @"\" + processPath; 

     FileVersionInfo info = FileVersionInfo.GetVersionInfo(processPath); 
     string processDesc = info.FileDescription; 
    } 
}