我对批处理很陌生,但我需要编写一个脚本来确定某个用户是否登录到服务器。为此,我使用
Query User
。这是我的代码:
@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
当我运行此程序时,它错误地告诉我帐户已登录,而我知道帐户尚未登录。为了确认这一点,我可以运行
query user <user account>
,它会给出以下输出,显示用户已断开连接:
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
我做错了什么?