I would like to know how to retrieve the list of users that are logged onto a Remote machine. I can do it with qwinsta /server:xxxx, but would like to do it in C#.
check out wmi in .net under system.management.
something like:
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()); }
You may not need the ConnectionOptions...
I ended up using the qwinsta /server:server1 command from C# -- it was much easier
thanks Ken
All this checks all 8 servers at the same time -- I put the result in sql server
but you can do what ever you want
query_citrix.bat script cd C:.......\bin\citrix_boxes qwinsta -server:servername or 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 sServer
};
}
else
{
bFirst = false;
}
}
}
}