批处理(.bat)转EXE后无响应:远程会话无锁屏退出工具故障
Let’s break down your problem: you’ve got a working batch script that stops the main screen from locking when exiting a remote session, but you have to manually minimize the console window. When you converted it to an EXE, it stopped working entirely. Here’s how to fix both issues—get a functional, windowless EXE that does the job seamlessly.
Why the Converted EXE Isn’t Working
Most batch-to-EXE tools struggle with scripts that request admin privileges (your UAC prompt logic) or rely on console-specific commands. If the tool doesn’t properly handle the runas elevation or redirect output, the EXE will appear unresponsive or fail silently.
Solution 1: Wrap the Batch in a VBScript (No EXE Conversion Needed)
This lets you run the batch completely hidden—no console window at all. You can even pin the VBS to your taskbar directly, or convert the VBS to an EXE later if you prefer.
- Save your batch script as
NoLockOnExit.bat(fix the tinytimeouttypo first: change>nullto>nul):
set "params=%*" cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B ) IF EXIST "%PROGRAMFILES(X86)%" (set bit=x64) ELSE (set bit=x86) if %bit%==x64 ( echo 64 bit operating system detected ) if %bit%==x86 ( echo 32 bit operating system detected ) for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do ( %windir%\System32\tscon.exe %%s /dest:console ) timeout /t 2 >nul exit
- Create a new VBScript file named
RunNoLock.vbswith this code:
Set WshShell = CreateObject("WScript.Shell") WshShell.Run chr(34) & "C:\Full\Path\To\Your\NoLockOnExit.bat" & chr(34), 0, False
Replace C:\Full\Path\To\Your\NoLockOnExit.bat with the actual full path to your batch file.
- Pin the VBS to your taskbar: Right-click the VBS, select "Create shortcut", then right-click the shortcut > Properties > Shortcut tab > Change Icon (pick any icon you like), then drag the shortcut to the taskbar.
When you run it, you’ll only see the UAC prompt (if needed)—no console window—and the script will execute correctly.
Solution 2: Use a Reliable Batch-to-EXE Tool
If you must have an EXE, use a tool that handles admin elevation and console hiding properly. Bat To Exe Converter (free) is a solid choice:
- Open the tool, load your batch script.
- Go to the Options tab:
- Set Visibility to "Invisible".
- Check "Require administrator privileges".
- Go to the Version Info tab to set an icon and metadata if you want.
- Click "Compile" to generate the EXE.
This should preserve the admin elevation logic and run without a window.
Solution 3: Simplify the Batch Script (Optional)
Your system bit-checking is unnecessary for the core tscon functionality (it works on both x86 and x64). You can trim the script to make it leaner and less likely to cause conversion issues:
set "params=%*" cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B ) for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do ( %windir%\System32\tscon.exe %%s /dest:console ) timeout /t 2 >nul exit
内容的提问来源于stack exchange,提问作者SBarn

