Skip navigation

How can my scheduled batch tell if a user is logged on?


If your scheduled batch job needs to know if a user is logged on, call UserOn.

UserOn.bat returns a uon environment valiable, with a Y or N value. You can then test this variable:

call UserOn
if "%uon%"

"Y" goto UsrIsOn

 

If a user is logged on, the Shell is running. UserON.bat first determines the Shell value by running Reg and then uses tlist from the Resource kit to see if it is running.

UserOn.bat contains:

@echo off
setlocal
set uon=N
for /f "Tokens=*" %%i in ('reg -Quiet -ListValue "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell"') do call :shell "%%i"
endlocal&set uon=%uon%
goto end
:shell
set sv=%1
set sv=%sv:"REG_SZ =%
set sv=%sv:"=%
for /f "Tokens=2" %%i in ('tlist') do call :find "%%i"
goto end
:find
if "%uon%""Y" goto end
if /i "%sv%"==%1 set uon=Y
:end
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish