locked
Number of online users in a domain in ASP.NET. RRS feed

  • Question

  • User724169276 posted

    Hello,

    I have a requirement to create an application which will let a domain admin know about the number of online users under him.By the word online users means ,if the person is locked his workstation or logged-off , these kind of details.I am completely blank at this point.Please share some ideas about how to approach ?

    Moreover i got this below .bat file which may help me to find the details about the online users , not sure how it will behave in a web application.Please check the same:

    @echo off
    for /f %%a in (pcList.txt) do (
    echo ********************
    echo Host: %%a
    echo ********************
    For /F "Tokens=2* Delims==\" %%i In (
    'WMIC /Node:%%a ComputerSystem GET UserName /Value^|Find "="'
    ) Do Echo Current Logged in User ID for %%a is: %%j
    )
    echo ********************
    echo Sweep is Complete
    echo ********************
    pause

    Wednesday, November 5, 2014 2:55 AM

Answers

  • User1508394307 posted

    Ok you mean the bat reads the text file? In this case try something like this

    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 myOutput = listFiles.StandardOutput;
     listFiles.WaitForExit(2000);
     if (listFiles.HasExited)
      {
      string output = myOutput.ReadToEnd();
      this.processResults.Text = output;
     }

    Found in google: http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx 

    P.S.

    There is a command that could redirect an output from console to a text file. See: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true 

    So try "mybat.bat > result.txt"

    It should output instead of console to result.txt which you can read from aspx.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 5, 2014 3:57 AM
  • User39490543 posted

    but i can't see result.txt anywhere

    This file will be in the folder from which you ran the batch file. 

    note: I removed the unnecessary "pause" from the end of the batch file

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 5, 2014 9:35 AM
  • User1508394307 posted

    I think I know what is it.

    1) you have pause command in the end of file and bat is waiting for a key press. Get rid of last line in the bat-file.
    2) as I already told you above, it makes sense to change path of the text file in the bat-file to a full path or set the WorkingDirectory

    psi.WorkingDirectory = "...";

    otherwise you bat will not find pcList.txt

    3) makes sense to change last lines as 

    System.IO.StreamReader myOutput = listFiles.StandardOutput;
    while (!listFiles.HasExited)
    {
    string line = myOutput.ReadLine();
    if (line != null)
    this.processResults.Text += line + "<br />";
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 5, 2014 10:21 AM

All replies

  • User1508394307 posted

    Moreover i got this below .bat file which may help me to find the details about the online users ,

    Does it give you expected result? If yes, then just call bat file from aspx and read its output in aspx.

    Wednesday, November 5, 2014 3:10 AM
  • User724169276 posted

    smirnov

    Does it give you expected result?

    Yes but how to fetch the result from the bat to my application ?? That's where i feel a brainstorming problem.You create a bat file with the same code and a text file named  pcList and paste your ipv4 address.Now run the bat file you will get the result.

    Wednesday, November 5, 2014 3:12 AM
  • User1508394307 posted

    Googling returns mlns of results including this forum.

    To run bat file you simply need to call

    System.Diagnostics.Process.Start(@"c:\file.bat");

    Since your file outputs its result to pcList.txt you would need to read pcList.txt

    To avoid problems with path, it's better to set full path to pcList.txt, e.g.

    for /f %%a in (C:\pcList.txt) do (

    Wednesday, November 5, 2014 3:38 AM
  • User724169276 posted

    Googling returns mlns of results including this forum.

    With all due respect sir , i know how to execute a batch file using Process. , but try to understand the problem.

    pcList text file will contain the ip addresses followed by commas, of the users under the domain typically a CSV kind of.Its not the output.The output is shown in the cmd promt.I want the output of that batch file in the application , so that i can show the admin about the status of those ipaddress (computers).

    I hope you are getting what i am trying to say.

    Wednesday, November 5, 2014 3:42 AM
  • User1508394307 posted

    Ok you mean the bat reads the text file? In this case try something like this

    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 myOutput = listFiles.StandardOutput;
     listFiles.WaitForExit(2000);
     if (listFiles.HasExited)
      {
      string output = myOutput.ReadToEnd();
      this.processResults.Text = output;
     }

    Found in google: http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx 

    P.S.

    There is a command that could redirect an output from console to a text file. See: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true 

    So try "mybat.bat > result.txt"

    It should output instead of console to result.txt which you can read from aspx.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 5, 2014 3:57 AM
  • User724169276 posted

    Its showing empty in the output ... you have that bat file code right .. try it and see if you are getting the output ?

    Wednesday, November 5, 2014 8:33 AM
  • User1508394307 posted

    What did you try? The code which "reads" console or the code which redirects output to a file?

    Wednesday, November 5, 2014 9:11 AM
  • User724169276 posted

    i tried with the bat file which reads the console and checked the output string variable ,its showing empty.

    Wednesday, November 5, 2014 9:15 AM
  • User39490543 posted

    Its showing empty in the output ... you have that bat file code right .. try it and see if you are getting the output ?

    smirnov's approach works fine for me. result.txt contains the output of mybat.bat.

    Wednesday, November 5, 2014 9:21 AM
  • User724169276 posted

    smirnov's approach works fine for me. result.txt contains the output of mybat.bat.

    I am sorry for being stupid here , but i can't see result.txt anywhere Undecided.I wanted to directly see the output of the bat file in a string i suppose.

    Wednesday, November 5, 2014 9:26 AM
  • User1508394307 posted

    Its showing empty in the output ... you have that bat file code right .. try it and see if you are getting the output ?

    I gave you 2 examples, if first is not working - try another one. Moreover, check how many time it takes to execute bat from console. Maybe you said it is not working because WaitForExit(2000) is not enough and you need to set bigger value.

    Wednesday, November 5, 2014 9:28 AM
  • User1508394307 posted

    tmorton

    smirnov's approach works fine for me. result.txt contains the output of mybat.bat.

    I am sorry for being stupid here , but i can't see result.txt anywhere Undecided.I wanted to directly see the output of the bat file in a string i suppose.

    If you do

    mybat.bat > result.txt

    most likely result.txt is in your user directory (e.g. c.\users\user)

    if you do 

    c:
    cd c:\scripts
    mybat.bat > result.txt

    then result.txt will be in c:\scripts

    if you run it from asp.net

    mybat.bat > result.txt

    then result.txt will be in a folder of the IIS pool account  (see 1st example)

    So either set correct path before, or use > c:\scripts\result.txt 

    Wednesday, November 5, 2014 9:33 AM
  • User39490543 posted

    but i can't see result.txt anywhere

    This file will be in the folder from which you ran the batch file. 

    note: I removed the unnecessary "pause" from the end of the batch file

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 5, 2014 9:35 AM
  • User724169276 posted

    check the screenshot.

    the url i am using is :

    D:/My Docs/Break Management files/RealTime/test.bat

    Thats where my bat file is along with the textfile to get the ip address :

    If i simply run the bat file its working fine.

    Wednesday, November 5, 2014 9:36 AM
  • User1508394307 posted

    I think I know what is it.

    1) you have pause command in the end of file and bat is waiting for a key press. Get rid of last line in the bat-file.
    2) as I already told you above, it makes sense to change path of the text file in the bat-file to a full path or set the WorkingDirectory

    psi.WorkingDirectory = "...";

    otherwise you bat will not find pcList.txt

    3) makes sense to change last lines as 

    System.IO.StreamReader myOutput = listFiles.StandardOutput;
    while (!listFiles.HasExited)
    {
    string line = myOutput.ReadLine();
    if (line != null)
    this.processResults.Text += line + "<br />";
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 5, 2014 10:21 AM
  • User724169276 posted

    ok .. now m able to read the bat file.I will mark this thread as answer.Lets see what alternative we have in my other thread.

    Thank you so much for your effort.

    Wednesday, November 5, 2014 10:33 AM