2009-08-06 2 views
1

リモートマシンにログオンしているユーザーの一覧を取得する方法を知りたいと思います。私はqwinsta/server:xxxxでそれを行うことができますが、C#でやりたいと思います。Windowsユーザーのリスト

答えて

1

チェックアウトwmi in .net system.management

のようなもの:

ConnectionOptions conn = new ConnectionOptions(); 
conn.Authority = "ntdlmdomain:NAMEOFDOMAIN"; 
conn.Username = ""; 
conn.Password = ""; 

ManagementScope ms = new ManagementScope(@"\\remotecomputer\root\cimv2", conn); 
ms.Connect(); 

ObjectQuery qry = new ObjectQuery("select * from Win32_ComputerSystem"); 

ManagementObjectSearcher search = new ManagementObjectSearcher(ms, qry); 

ManagementObjectCollection return = search.Get(); 

foreach (ManagementObject rec in return) 
{ 
    Console.WriteLine("Logged in user: " + rec["UserName"].ToString()); 
} 

あなたは、私がqwinsta /サーバーを使用して終了ConnectionOptions ...

+0

私はそれがWMI_LogonSessionだと信じています - あなたはどこからでも参考になる例がありますか? –

+0

このコードは機能しますが、コンピュータにログオンしていないと動作しないようです。私は管理者である8台のサーバーを持っており、ログオンしているすべてのユーザーのリストを遠隔から取得したい。私はWin32_LogonSessionクエリを使用する必要があると思いますか?もしそうなら、それを使用する方法はわかっています。これは初めてのWMIの作業です。 –

+0

わかりません...これが役立つかどうかを確認してください: http://stackoverflow.com/questions/898757/vb-using-wmi-get-logged-in-users – klabranche

0

を必要としない場合がありますのC#からのserver1コマンド - それははるかに簡単

ましたありがとうKen

これはすべて同時に8つのサーバーをチェックします - 私は結果をSQLサーバーに入れます

しかし、あなたはあなたが欲しい、これまで何ができる

query_citrix.batスクリプト
CDのC:....... \ binに\ citrix_boxes
qwinsta -server:サーバー名またはIP> servername.txt


string sAppCitrixPath = Application.StartupPath.ToString() + "\\citrix_boxes\\"; 

//Run Script for current citrix boxes 
Process proc = new Process(); 
ProcessStartInfo si = new ProcessStartInfo(); 
si.FileName = sAppCitrixPath + "query_citrix.bat"; 
proc.StartInfo = si; 
proc.Start(); 
proc.WaitForExit(); 
int exitCode = proc.ExitCode; 
proc.Close(); 

if (exitCode == 0) 
{ 
     //Execute update who is on the Citrix_Boxes Currently 
     DirectoryInfo dic = new DirectoryInfo(sAppCitrixPath); 
     FileInfo[] fic = dic.GetFiles("*.txt"); 
     for (int i = 0; i < fic.Length; i++) 
     { 
     ParseQWinStaServerFile(fic[i].FullName.ToString(), fic[i].Name.ToString().ToUpper().Replace(".TXT","")); 
     File.Delete(fic[i].FullName.ToString()); 
     } 
} 

    private void ParseQWinStaServerFile(string sLocation,string sServer) 
    { 
     using (StreamReader sr = File.OpenText(sLocation)) 
     { 
      string sRecord = String.Empty; 
      char[] cSep = new char[] {' '}; 

      bool bFirst = true; 
      while ((sRecord = sr.ReadLine()) != null) 
      { 
       if (bFirst == false) 
       { 
        string[] items = sRecord.Split(cSep, StringSplitOptions.RemoveEmptyEntries); 
        //Make sure all columns are present on the split for valid records 
        if (sRecord.Substring(19, 1) != " ") // check position of user id to see if it's their 
        { 

         //Send the user id and server name where you want to. 
         //here is your user 
         id = items[1].ToString().ToLower().Trim() 
         //here is your server 
        }; 
       } 
       else 
       { 
        bFirst = false; 
       } 
      } 
     } 
    } 
関連する問題