I have created a .bat file that displays a servers terminal Service SessionID and Username. Im Displaying the 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 the SessionID and Username that works 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];
//row[1] = item.Split(',')[1];
table2.Rows.Add(row);
//en.MoveNext();
//break;
}
}
this.displayUsers.DataSource = table2;
this.displayUsers.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
}
The Error i get is: Enumeration has either not started or has already finished.
I set a break point and it seams that the while loop completes but starts over and after it adds a couple duplicate records it throws the error. any ideas on whats be causing this. Im thinking its my RegEx