I created a .bat file that displays the server terminal Service SessionID and Username. Im displaying information in a datagrid
Here is the output of the .bat file:
C: \ Documents and Settings \ adcock> qwinsta / server: ilsap01
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console 0 Conn wdcon
rdp-tcp 65536 Listen rdpwd
Jrodriguez 27 Disc rdpwd
pbahena 8 Disc rdpwd
tfurr 3 Disc rdpwd
rdp-tcp # 2187 kchild 14 Active rdpwd
Trhodes 10 Disc rdpwd
ajordan 16 Disc rdpwd
Trhodes 11 Disc rdpwd
rdp-tcp # 2191 rluna 15 Active rdpwd
rdp-tcp # 2192 lcathey 17 Active rdpwd
the only information I want to display is SessionID and Username, which work somewhat with the code below.
protected void Button1_Click(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader rawUserData = listFiles.StandardOutput;
listFiles.WaitForExit(30000);
try
{
DataTable table2 = new DataTable();
table2.Columns.Add(new DataColumn("UserName", typeof(string)));
table2.Columns.Add(new DataColumn("SessionId", typeof(string)));
String myString = rawUserData.ReadToEnd();
string exp = @"([\w_]+)"; ;
MatchCollection matches = Regex.Matches(myString, exp,RegexOptions.IgnoreCase);
IEnumerator en = matches.GetEnumerator();
while (en.MoveNext())
{
Match match = (Match)en.Current;
if (en.Current.ToString() == "rdpwd")
{
en.MoveNext();
Match match_Item = (Match)en.Current;
string item = match_Item.Value;
en.MoveNext();
Match match_Item2 = (Match)en.Current;
string item2 = match_Item2.Value;
DataRow row = table2.NewRow();
row[0] = item.Split()[0];
row[1] = item2.Split()[0];
table2.Rows.Add(row);
}
}
this.displayUsers.DataSource = table2;
this.displayUsers.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
}
The error I get is: Enumeration either has not started, or has already been completed.
I set a breakpoint and it fades out that the while loop ends but starts, and after it adds a couple of duplicate entries, it throws an error. any ideas on what might be causing this. I think my RegEx
source
share