1

In Windows XP, I created a Scheduled Task to run every 15 minutes, and it is working perfectly fine. But I don't want the task to run when the computer is locked – CtrlAltDel.

Is there a way to prevent the scheduled task from running when the system is locked?

2 Answers 2

0

See Win32_LogonSession Win32_LogonSession class, LogonType.

Logntype.cmd:

@ECHO OFF
@FOR /F "tokens=4 delims==" %%L IN ('wmic path win32_loggedonuser^|find /I "%username%"') do @SET /A LogonId=%%L
@echo LogonId=%LogonId%
@FOR /F %%T IN ('wmic path win32_logonsession Where ^(LogonId^=%LogonId%^) get LogonType^|more /E +1') do @SET /A LogonType=%%T &GOTO SkipLineLogonType
@:SkipLineLogonType
@echo LogonType=%LogonType%

@IF %LogonType% EQU 2  (
       @ECHO Interactive
       :: Interactive Code!
      ) ELSE (

       @ECHO Your Code!
       :: not interactive logon
      )

Case Of emulation:

   @IF %LogonType% EQU 2  (
       @ECHO Interactive
       @ECHO Interactive Code!

   )

   @IF %LogonType% EQU 7  (
       @ECHO Interactive
       @ECHO Interactive Code!

   )

Numeric value that indicates the type of logon session:

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: LogonType :
::
:: Value  Meaning 
::
:: 0 Used only by the System account.
:: 
::   Interactive
:: 2 Intended for users who are interactively using the machine, such as a user being logged on by a terminal server, remote shell, or similar process.
::    
::   Network
:: 3 Intended for high-performance servers to authenticate clear text passwords. LogonUser does not cache credentials for this logon type.
::  
::   Batch
:: 4 Intended for batch servers, where processes can be executed on behalf of a user without their direct intervention; or for higher performance servers that process many clear-text authentication attempts at a time, such as mail or web servers. LogonUser does not cache credentials for this logon type.
::  
::   Service
:: 5 Indicates a service-type logon. The account provided must have the service privilege enabled.
::  
::   Proxy
:: 6 Indicates a proxy-type logon.
::  
::   Unlock
:: 7 This logon type is intended for GINA DLLs logging on users who are interactively using the machine. This logon type allows a unique audit record to be generated that shows when the workstation was unlocked. 
::  
::   NetworkCleartext
:: 8 Windows Server 2003, Windows 2000, and Windows XP:  Preserves the name and password in the authentication packages, allowing the server to make connections to other network servers while impersonating the client. This allows a server to accept clear text credentials from a client, call LogonUser, verify that the user can access the system across the network, and still communicate with other servers. 
:: 
::   NewCredentials
:: 9 Windows Server 2003, Windows 2000, and Windows XP:  Allows the caller to clone its current token and specify new credentials for outbound connections. The new logon session has the same local identify, but uses different credentials for other network connections. 
:: 
::    RemoteInteractive
:: 10 Terminal Services session that is both remote and interactive.
::  
::    CachedInteractive
:: 11 Attempt cached credentials without accessing the network.
::  
::    CachedRemoteInteractive
:: 12 Same as RemoteInteractive. This is used for internal auditing.
::  
::    CachedUnlock
:: 13 Workstation logon.
:: 
0

Quick and dirty solution:

Find an appropriately protected folder in which a "workstation lock file" can be created that other .bat files can test the existence of.

Create a file createLockFile.bat in this folder containing the following code:

@echo off
@echo "" > WORKSTATION_IS_LOCKED

Create another file deleteLockFile.bat in this folder containing the following code:

@echo off
del WORKSTATION_IS_LOCKED 2>nul

Create a task that triggers on workstation lock to run createLockFile.bat and another task that triggers on workstation unlock to run the deleteLockFile.bat. Make sure to set the Start in: value to the path of the containing folder.

Now you can use the following code at the top of any scheduled .bat file to exit out early if the workstation is locked:

if exist "<containing folder path>\WORKSTATION_IS_LOCKED" (
    exit /b
)

See here for allowing the .bat files to execute "invisibly" (ie, without having a window briefly appear)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .