始终位于顶部的批处理脚本

Den*_*ils 1 powershell batch-file

我创建了一个简单的批处理脚本来使用以下代码伪锁定计算机:

@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.
Run Code Online (Sandbox Code Playgroud)

我希望此窗口保持在顶部,并且最好在到达文件末尾之前不可关闭。但是,我还没有找到方法来做到这一点。

小智 5

您可以调用 PowerShell,而 PowerShell 又可以调用 WinAPI...至少在 Windows 8+ 上(7 也可能有效,以前的版本可能不行)。

这相对简单:

  1. 调用 PowerShell
  2. 让它独立于上下文运行
  3. 用于SetWindowPos将窗口置于前面
  4. 用于GetConsoleWindow找出要对哪个窗口执行操作

这一切都非常适合一个命令:

@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)"
Run Code Online (Sandbox Code Playgroud)