1

I have created a simple batch script to pseudo-lock a computer using the following code:

@ECHO OFF & setlocal ENABLEDELAYEDEXPANSION
setlocal EnableDelayedExpansion
color a
TITLE Lock
if not "%1" == "max" (
powershell -command "& { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }"
start /MAX cmd /c %0 max & exit/b
)
:Lock 
echo Please enter a password to lock your computer . . .
powershell -Command $pword = read-host "Enter password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > EOFlock.txt & set /p Pass1=<EOFlock.txt & del EOFlock.txt
TITLE Lock
taskkill /IM explorer.exe /F >nul
cls
echo Please type the password to unlock the computer . . .
:Locked 
set /p Pass2=
:Unlock 
if !Pass1! == !Pass2! (goto End)
goto Locked 
:End
start explorer.exe
echo This Computer is unlocked.

I want this window to stay on top, and preferably be unclosable until it has reached the end of the file. However, I did not find a way to do this yet.

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • 1
    Actually I don't think there is a way in pure batch... it might be possible with VBScript or JavaScript... is such is suitable for you, you should edit your post an add the respective tag(s)... – aschipfl Sep 14 '15 at 05:52
  • Concerning an implementation in PowerShell, you might be interested in [this post](http://stackoverflow.com/a/12802050)... – aschipfl Sep 14 '15 at 07:41
  • Instead of "pseudo-locking" the screen, why don't you actually [lock the screen](https://technet.microsoft.com/en-us/library/dd315249.aspx)? – Ansgar Wiechers Sep 14 '15 at 18:42
  • @Dennis you might find [this answer](http://stackoverflow.com/a/37912693/1683264) useful if you're still looking. – rojo Jun 19 '16 at 23:35

1 Answers1

5

You can call into PowerShell which in turn can call into the WinAPI... at least on Windows 8+ (7 might work too, previous versions probably not).

It's relatively straightforward:

  1. Call PowerShell
  2. Tell it to run independent of context
  3. Use SetWindowPos to bring a window to the front
  4. Use GetConsoleWindow to find out which window to act on

It all fits pretty neatly into a single command:

@powershell -ExecutionPolicy UnRestricted -Command "(Add-Type -memberDefinition \"[DllImport(\"\"user32.dll\"\")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x,int y,int cx, int xy, uint flagsw);\" -name \"Win32SetWindowPos\" -passThru )::SetWindowPos((Add-Type -memberDefinition \"[DllImport(\"\"Kernel32.dll\"\")] public static extern IntPtr GetConsoleWindow();\" -name \"Win32GetConsoleWindow\" -passThru )::GetConsoleWindow(),-1,0,0,0,0,67)"
Spooky
  • 2,966
  • 8
  • 27
  • 41