远程Windows用户列表

时间:2009-08-06 01:42:22

标签: list

我想知道如何检索登录到远程计算机的用户列表。我可以用qwinsta / server:xxxx来做,但是想在C#中做。

2 个答案:

答案 0 :(得分:1)

system.management下查看.net中的wmi。

类似的东西:

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()); 
}

您可能不需要ConnectionOptions ...

答案 1 :(得分:0)

我最终使用来自C#的qwinsta / server:server1命令 - 它更容易

谢谢肯

所有这些同时检查所有8台服务器 - 我把结果放在sql server

但你可以做任何你想做的事情

query_citrix.bat脚本
cd C:....... \ bin \ citrix_boxes
qwinsta -server:servername或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;
                }
            }
        }
    }