如何在登录后经过‘x’时间后锁定计算机

如何在登录后经过‘x’时间后锁定计算机

虽然有一个非常类似问题:如何在登录后一定时间后锁定我的计算机?我觉得它不适合我的需求。我需要我的电脑锁定未关闭

我做了什么:用以下脚本制作了一个 bat 文件

shutdown -l /t 1800

当我在任务计划程序中设置任务时,它根本没有我的电脑。bat 文件本身不起作用。我不知道为什么。

如果我做。

shutdown -l

它运行完美。但我不希望这种情况发生。我希望它在特定时间后执行此操作。

我确实知道 WinKey + L 和 Signing Out 是不同的。

另外,除了周六和周日以外,有没有办法每天都这样做,因为周六和周日这段时间我必须工作。

答案1

  1. 运行任务计划程序。
  2. 创建一个在登录后给定的时间内激活的计划任务 - 在触发器选项卡上,有一个登录触发器和延迟事件后操作的选项 - 在这里您可以输入您的时间。
  3. 查看以最高权限运行(特别是如果没有此选项则锁定不起作用)。
  4. 将您的 BAT 文件作为可运行的操作。

完毕。

您无法在 UI 中添加周末例外,但您可以直接在批处理文件或 ps1 文件中测试工作日。或者测试是否存在“do-not-lock”文件。如果存在,则结束批处理而不锁定。使用计划任务(在工作日/周末运行)创建/删除该文件。

答案2

参考这个旧的 vbscript自动锁定_空闲_超时.vbs


我对其进行了一些改动,以便循环运行,并且每 30 分钟锁定一次计算机。


用户定义的时间后自动锁定.vbs

'#################################################################################
'#       Script Name : Auto-Lock_After_Amount_of_Time_Defined_by_User.vbs        #
'#################################################################################
Option Explicit
Dim Copyright,Msg
Dim Timeout,strCommand,VbsPath,ShortcutName
Timeout = 30 '30 minutes : You can modify this variable as your convenience
Msg = Lang
Copyright = Msg(0) & ChrW(169) &" Hackoo 2022"
If AppPrevInstance() Then 
    MsgBox Msg(1) & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,Copyright
    WScript.Quit
Else 
    Do
        strCommand = "CMD /C Shutdown -L"
        VbsPath = Wscript.ScriptFullName
        ShortcutName = "Auto-Lock_TimeOut"
        Call Shortcut(VbsPath,ShortcutName)
        Wscript.Sleep 1000 * 60 * Timeout
        CreateObject("Wscript.Shell").Run strCommand,0,True
    Loop
End If
'---------------------------------------------------------------------------------------------------------------
Sub Shortcut(PathApplication,ShortcutName)
    Dim objShell,StartFolder,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If ShortcutName = "" Then
        ShortcutName = MyTab(UBound(MyTab))
    End if
    StartFolder = objShell.SpecialFolders("Startup")
    Set objShortCut = objShell.CreateShortcut(StartFolder & "\" & ShortcutName & ".lnk")
    objShortCut.TargetPath = DblQuote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,44"
    objShortCut.Save
End Sub
'---------------------------------------------------------------------------------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'---------------------------------------------------------------------------------------------------------------
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function    
'----------------------------------------------------------------------------------------------------------------
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'----------------------------------------------------------------------------------------------------------------
Function OSLang()
    Dim dtmConvertedDate,strComputer,objWMIService,oss,os
    Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    For Each os in oss
        OSLang = os.OSLanguage
    Next
End Function
'----------------------------------------------------------------------------------------------------------------
Function Lang()
Dim MsgFR,MsgEN

MsgFR = Array("Verrouillage automatique en cas d'inactivité " ,"ATTENTION ! Il y a une autre instance en cours d'exécution !",_
"Attention ! Votre Session va être verrouillé dans 20 secondes !`n`n Voulez-vous confirmer le Verrouillage de votre session ?","`n`n OUI ou NON ?")

MsgEN = Array("Automatic Lock Session On Idle TimeOut ","ATTENTION ! There is another instance running !",_
"Warning ! Your Session will be locked in 20 seconds !`n`n Do you want to confirm the Locking of your session ?","`n`n YES or NO ?")

'Vérifiez si le système est français pour définir le tableau français comme message, sinon définissez-le comme anglais
'Check if the system is french to set the French array as message otherwise set it as English

If Oslang = 1036 Then
    Lang = MsgFR ' French Array Message to be set
Else
    Lang = MsgEN ' English Array Message to be set
End If
End Function
'----------------------------------------------------------------------------------------------------------------

答案3

只需将其复制粘贴到你的 bat 中即可:

call timeout /t 1800 && Rundll32.exe user32.dll,LockWorkStation

如果你想让你的球棒安静地运行,请遵循本指南将其添加到任务计划程序之前

答案4

感谢大家的回复,每个人都给了我一些启发。以下是完整的答案:

$i = 0
$sTime = 0;
$day = (Get-Date).DayOfWeek

if ($day -match "Saturday|Sunday") {
    $sTime = 6000
} else {
    <#Whatever you need to do#>
}

Write-Host "Screen time allocation on a $day : $sTime"

while($i -ne $sTime)
{
    $remainSTime = $sTime - $i
    $ts =  [timespan]::fromseconds($remainSTime)
    Write-Host $ts
    Start-Sleep -Seconds 1
    $i++
}
Rundll32.exe user32.dll,LockWorkStation

这就是完整的答案。@Hackoo 的答案很好,但 vbs 脚本非常冗长且难以理解。作为一个关心安全的用户,我决定尝试理解脚本 :)

相关内容