防止系统锁定时运行计划任务

防止系统锁定时运行计划任务

在 Windows XP 中,我创建了每 15 分钟运行一次的计划任务,它运行得很好。但我不想在计算机锁定时运行该任务 – CtrlAltDel

有没有办法可以在系统锁定时阻止计划任务运行?

答案1

Win32_Logon会话Win32_LogonSession 类,LogonType。

登录类型.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
      )

模拟案例:

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

   )

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

   )

指示登录会话类型的数字值:

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 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.
:: 

答案2

快速而肮脏的解决方案:

找到一个适当保护的文件夹,在其中创建一个“工作站锁定文件”,其他 .bat 文件可以测试其存在。

createLockFile.bat在此文件夹中创建一个包含以下代码的文件:

@echo off
@echo "" > WORKSTATION_IS_LOCKED

在此文件夹中创建另一个文件,deleteLockFile.bat其中包含以下代码:

@echo off
del WORKSTATION_IS_LOCKED 2>nul

创建一个在工作站锁定时触发运行的任务createLockFile.bat,以及另一个在工作站解锁时触发运行的任务deleteLockFile.bat。确保将Start in:值设置为包含文件夹的路径。

现在,您可以在任何计划的 .bat 文件的顶部使用以下代码,如果工作站被锁定则提前退出:

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

这里允许 .bat 文件“隐形”执行(即,不会短暂出现窗口)

相关内容