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
You are calling MoveNext twice inside the loop. That is causing the problem.
When you are in the last row, you call MoveNext, the it finds "rdpwd", then it calls MoveNext one time, then it calls it again, and this is when the enumerator becomes invalid. In then last MoveNext in the while it breaks because it's already at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With