13

machine get locked during the automation script run-time , this is because there is a long interval between different scripts that(this is required for some reasons) . I want to avoid this auto locking feature. The problem is, as per security policies we cannot disable this feature from control panel. Is there any other way to keep the system unlocked?

11 Answers 11

27

I did this...It sends 2 SCROLLLOCK Keys, quickly, so it wont interfere (well, not anything serious anyways) with any applications. It does this every 60 seconds....Cheers!

set wsc = CreateObject("WScript.Shell")
Do
WScript.Sleep (60*1000)
wsc.SendKeys ("{SCROLLLOCK 2}")
Loop

To stop it, open taskmanager and terminate the wscript.exe process. (If there are multiple, use the Command Line column to figure out which process)

5
  • Best solution out of all tested. Using it for quite a while to avoid enforced screensaver - no need to modify setting/registry, no need to install anything, just using simply native OS functionality.
    – R1cky
    Jul 15, 2014 at 11:18
  • Works nice. Once executed, how do I stop it?
    – Danijel
    Aug 25, 2017 at 11:08
  • will this work in Virtual desktop? I have tried out but no luck. Mar 20, 2018 at 15:21
  • Virtual Desktops is just a UI layer on the shell. It should work. Did you mean Virtual Machine? If yes, the script needs to be running inside the VM not the host.
    – st0le
    Mar 20, 2018 at 16:56
  • Still working on Windows 10, even when locking feature is set by admin. Thanks mate.
    – itachi
    May 7, 2019 at 11:41
6

I'd think that you could halt the locking by sending a keypress at regular intervals, so I'd suggest looking at WScript.SendKeys. Then put that in a loop with a sleep to make it send it regularly.

Just be careful about what key you're sending so you don't affect anything else though.

2
  • Thanks .. is there any other way other than sendkeys ? as you said it is risky, because application will be in open state , if any key is pressed, there could be a problem with data input.
    – Srivi
    Dec 16, 2010 at 6:50
  • @Srivigneshwar: You could try simulating mouse movement. But I'm not sure if there's any way of doing that from VBScript so you might have to write that code in another language and then call it from the script. Or you could try sending in keys that doesn't do very much, like the Scroll Lock or similar. All in all, you might have to try various things until you find what helps you. Dec 16, 2010 at 8:10
6

I use a pair of files...

The first file (Keep_Awake__start.vbs) keeps things awake/unlocked. The second file (Keep_Awake__end.vbs) conveniently terminates the process when you want to go back to a normal proces.

The first file...

' Keep_Awake__start.vbs
' Graham Edwards

' Typical stored in Start Menu 
'
' Use Keep_Awake__start.vbs to keep the computer from inactivity-based lockouts.
' Use Keep_Awake__end.vbs to remove Keep_Awake__start.vbs

' Largely pulled from st0le response
' Http://stackoverflow.com/questions/4457907/how-to-prevent-auto-locking-feature-of-an-xp-machine-using-vbscript

' --- Define Object
    set wsc = CreateObject("WScript.Shell")

' --- Loop every so many minutes and change the scroll lock setting 
    Do
        ' Wait for ~2 minutes
        WScript.Sleep (2*60*1000)
        wsc.SendKeys ("{SCROLLLOCK 2}")
    Loop

The second file...

' Keep_Awake__end.vbs
' Graham Edwards

' Use Keep_Awake__start.vbs to keep the computer from inactivity-based lockouts.
' Use Keep_Awake__end.vbs to remove Keep_Awake__start.vbs

' Largely pulled from Ansgar Wiechers response
' http://stackoverflow.com/questions/22324899/kill-a-vbscript-from-another-vbscript

' --- Define Object
    Set wmi = GetObject("winmgmts://./root/cimv2")

' --- Search and Destroy
    qry = "SELECT * FROM Win32_Process WHERE Name='wscript.exe' AND NOT " & _
    "CommandLine LIKE '%" & Replace(WScript.ScriptFullName, "\", "\\") & "%'"

    For Each p In wmi.ExecQuery(qry)
      p.Terminate
    Next

' --- Clean up
    Set wmi = Nothing ' Release the Application object

The files can both be created from a regular text editor and stored anywhere (like your desktop). Once you save the file with a .vbs file extension, it is executable. So, you just have to double click on the file icon to get things to start or end (depending on which file you double click).

You could store the Keep_Awake__start.vbs in the Windows Startup folder, so it launches as soon as you log in.

2
  • @Graham_Edwards Both scripts are executing fine for start and end but I don't see the Scroll Lock light up and light off every 2 minutes while start script is running, is this expected behavior?
    – Suresh
    Dec 11, 2017 at 9:49
  • Yes, I believe that is expected behavior. If you want to see it flash (for say, half a second), then replace: wsc.SendKeys ("{SCROLLLOCK 2}") with wsc.SendKeys ("{SCROLLLOCK}") WScript.Sleep (500) wsc.SendKeys ("{SCROLLLOCK}") Jan 23, 2020 at 0:22
1

The simplest solution that works is to open a mp3 or video file and play it continuously in windows media player (use the repeat feature and mute the volume in the player). In this way the screensaver never comes or the system never locks.

1
  • This is not working in my machine, though not sure of the cause
    – Suresh
    Dec 11, 2017 at 9:50
0

If this is while running your automation suite, then use your automation suite to do a mouse twitch every so often. What I find works well is if you have your own custom sleep function, and as part of your sleep function, you twitch the mouse. Use this for your long interval between scripts.

0

Use the following VB script code to keep your machine unlocked:

Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Control Panel\Desktop\ScreenSaverIsSecure", "0" , "REG_SZ"
Wscript.Echo "Your machine remains unlocked"

Cheers!!

0

I have tried the shell scripts and scheduling the simulation of mouse movements with task Scheduler - but here's what I found was the BEST of the lot. +1 for its simplicity. It automatically moves the mouse to a shifted pixel, so the screen does not auto-lock(though you have options to configure this, should the need be)

Reference: Auto Mouse Mover - http://www.murgee.com/auto-mouse-mover/

0
Set wshShell =wscript.CreateObject("WScript.Shell")
do
  wscript.sleep 55000
  wshshell.sendkeys "{SCROLLLOCK}"
loop

We can use this code to prevent window to unlock again.

0

I've done it this way

(outside loop = declared variable)

'For 64 bit Excel.
Private Declare PtrSafe Function SetCursorPos Lib "user32" _
                        (ByVal x As Long, _
                        ByVal y As Long) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

'For 32 bit Excel.
Private Declare Function SetCursorPos Lib "user32" _
                        (ByVal x As Long, _
                        ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


 ' default variable
 private mousecounter as integer

(inside procedure)

Dim llCoord As POINTAPI
Dim activeXMousePos As Integer
Dim activeYMousePos As Integer

GetCursorPos llCoord
activeXMousePos = llCoord.Xcoord
activeYMousePos = llCoord.Ycoord

(inside loop):

        If mousecounter <= 2 Then
            SetCursorPos activeXMousePos - 1, activeYMousePos - 1
            mousecounter = mousecounter + 1
        End If

        If mousecounter > 2 Then
            SetCursorPos activeXMousePos + 2, activeYMousePos + 2
            mousecounter = 0
        End If

So - basically this code moves mouse 1 px leftup and 1 px rightdown lor each loop interval. You can declare any other values but I've found that this mouse move don't bother users.

0

if anyone needs the declarations with conditions

#If vba7 and win64 then

'For 64 bit Excel.
Private Declare PtrSafe Function SetCursorPos Lib "user32"  (ByVal x As Long,  ByVal y As Long) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


#Else

'For 32 bit Excel.
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,  ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

#End If
0

I have made a very simple and easy to use script(.bat) which disabled autolock by running the script in a console(cmd) window. If you want to stop the script, the user just need to close the console window. #disable_autolock_stackoverflow_mr_v

 :: SAVE this script as a .bat file and execute it to disable autolock   
 @echo off
 COLOR 0A
 mode 69,9
 title Stay Awake
 echo Dim objResult > %temp%\stay_awake.vbs
 echo Set objShell = WScript.CreateObject("WScript.Shell")    >> %temp%\stay_awake.vbs
 echo Do While True >> %temp%\stay_awake.vbs
 echo  objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}") >> %temp%\stay_awake.vbs
 echo  Wscript.Sleep (5000) >> %temp%\stay_awake.vbs
 echo Loop >> %temp%\stay_awake.vbs
 echo Start Time: %date% %time%
 ECHO Please close this window to stop the Stay_Awake Script
 echo.
 cscript %temp%\stay_awake.vbs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.