0

I am very new to batch but I need to write a script that can determine whether a certain user is logged into the server or not. To do this, I am using Query User. Here is my code:

@echo off

set "account=<account name>"
set "output=C:\Login.txt"

query user %account% > temp.txt

findstr /i "%account%" temp.txt > nul
if %errorlevel% equ 0 (
    findstr /i "Active" temp.txt > nul
    if %errorlevel% equ 0 (
        echo User %account% is logged in. > %output%
    ) else (
        echo User %account% is not logged in. > %output%
    )
) else (
    echo User %account% not found. > %output%
)

del temp.txt

When I run this, it incorrectly tells me an account is logged in when I know it is not. To confirm this, I can run query user <user account>, which gives me this output showing that the user is disconnected:

C:\>query user <user account>
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 <account name>                           34  Disc         2:24  8/2/2023 9:55 AM

What am I doing wrong?

Jon Zavialov
  • 231
  • 1
  • 10
  • 1
    Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) Specifically, since the inner `%errorlevel%` is getting set and used inside of the outer `if` statement, it's technically still looking at the value of `%errorlevel%` from the first `findstr`, and you need to add `setlocal enabledelayedexpansion` to the top of your script and use `!errorlevel!` instead. – SomethingDark Aug 02 '23 at 22:11
  • 2
    Of course you could simply change both `if %errorlevel% equ 0` lines to read `if not errorrlevel 1` instead. – Compo Aug 02 '23 at 22:40
  • 1
    Please open a [command prompt](https://www.howtogeek.com/235101/), run `if /?` and read the output usage help. There is explained already on first page which syntax to use for evaluation of the exit code of a command or executable by using `IF [NOT] ERRORLEVEL number command`. You will not find any information about `if %errorlevel% equ`. The reason is that the documented syntax always works while the syntax used in your batch file works only under certain conditions. – Mofi Aug 03 '23 at 05:16
  • 1
    The used syntax does not work if there is by chance an __environment__ variable with name `ERRORLEVEL` defined or if the value of the __dynamic__ variable `ERRORLEVEL` is modified and referenced with `%ERRORLEVEL%` inside a command block beginning with `(` and ending with matching `)`. There should be used the documented syntax which always works. It would be also very easy to avoid the command blocks completely, for example by using [conditional command operators](https://stackoverflow.com/a/25344009/3074564). – Mofi Aug 03 '23 at 05:20
  • 1
    There can be used below `@echo off` and the two environment variable definitions the following three command lines. __ATTENTION:__ There is no space left to last redirection operator `>` and the command operator `&` for avoiding a trailing space in the output file. – Mofi Aug 03 '23 at 05:39
  • 1
    __1.__ `%SystemRoot%\System32\query.exe user "%account%" 2>nul | %SystemRoot%\System32\findstr.exe /I /R /C:">%account% .* ACTIVE " >nul && (echo User %account% is logged in.>"%output%"& exit /B)` – Mofi Aug 03 '23 at 05:39
  • 1
    __2.__ `%SystemRoot%\System32\query.exe user "%account%" 2>nul | %SystemRoot%\System32\findstr.exe /I /R /C:">%account% " >nul && (echo User %account% is not logged in.>"%output%"& exit /B)` – Mofi Aug 03 '23 at 05:40
  • 1
    __3.__ `echo User %account% not found.>"%output%"` – Mofi Aug 03 '23 at 05:40
  • 1
    I should add that `echo User %account%` is problematic on user account is a string like `Obelix GmbH & Co. KG` because of `&` in the account name is interpreted as unconditional command operator because of `%account%` is not enclosed in `"` to write it into the file also without `"`. It would be better to enclose all `%account%` in `"` or if that is not wanted for whatever reason, insert after the environment variable definitions the command line `setlocal EnableExtensions EnableDelayedExpansion` and replace in the three lines `%account%` by `!account!` and `%output%` by `!output!`. – Mofi Aug 03 '23 at 05:47

1 Answers1

0

Thank you to everyone for your help. I have combined some of the comments to create the following script. The purpose of this is to execute another batch file only if a certain user is logged out:

@echo off
setlocal

set "account=<account name>"
set "output=C:\Login.txt"

%SystemRoot%\System32\query.exe user "%account%" 2>nul | %SystemRoot%\System32\findstr.exe /I /R /C:"%account% .* ACTIVE " >nul
if errorlevel 1 (
    echo User %account% is not logged in.>"%output%"
    call another_script.bat
) else (
    echo User %account% is logged in.>"%output%"
)

endlocal
Jon Zavialov
  • 231
  • 1
  • 10