批处理语句@if (@CodeSection == @Batch) @then功能问询
@if (@CodeSection == @Batch) @then in Mixed Batch/JScript Files Great question! That line is a clever hack to pack batch script logic and JScript functionality into a single file—something Windows supports natively via the Windows Script Host (WSH). Let’s break down exactly what it does and how the rest of your reference code uses it:
What the Line Does
Let’s parse it piece by piece:
@at the start: Suppresses command echoing, so the line itself doesn’t show up when the script runs (keeps your output clean).@CodeSection == @Batch: These are special predefined variables in Windows scripting. The condition checks if the script is currently being interpreted as a batch file (bycmd.exe) rather than as JScript (bycscript.exe/wscript.exe).@then: If the condition is true (we’re in batch mode), tellscmd.exeto execute all the code that follows until it hits the@endmarker.
How the Full Script Works
Your reference code is a hybrid script—here’s the split:
1. Batch Section (Above @end)
This is what cmd.exe runs first. It handles the "batch" tasks:
- Defines
%SendKeys%as a shortcut to run the same script file, but instructcscript.exeto interpret it as JScript (using//E:JScript). - Starts a new
cmdwindow in the background. - Uses the
%SendKeys%shortcut to send keystrokes (like{ENTER},{UP}) to the active window. - Uses
pingas a simple delay (waiting 5 seconds before sending the next set of keys).
2. JScript Section (Below @end)
This part is completely ignored by cmd.exe (it stops parsing at @end). When the batch script calls cscript.exe with the same file, WSH reads this JScript code, which uses the WshShell.SendKeys() method to send the specified keystrokes to the foreground window—this is how the actual keyboard input simulation happens.
Here’s your full reference code formatted for clarity:
Press Keyboard keys using a batch file @if (@CodeSection == @Batch) @then @echo off rem Use %SendKeys% to send keys to the keyboard buffer set SendKeys=CScript //nologo //E:JScript "%~F0" rem Start the other program in the same Window start "" /B cmd %SendKeys% "echo off{ENTER}" set /P "=Wait and send a command: " < NUL ping -n 5 -w 1 127.0.0.1 > NUL %SendKeys% "echo Hello, world!{ENTER}" set /P "=Wait and send an Up Arrow key: [" < NUL ping -n 5 -w 1 127.0.0.1 > NUL %SendKeys% "{UP}" set /P "=] Wait and send an Enter key:" < NUL ping -n 5 -w 1 127.0.0.1 > NUL %SendKeys% "{ENTER}" %SendKeys% "exit{ENTER}" goto :EOF @end // JScript section var WshShell = WScript.CreateObject("WScript.Shell"); WshShell.SendKeys(WScript.Arguments(0));
Why This Trick Matters
Batch scripts don’t have built-in support for sending keystrokes, but JScript (via WSH) does. This hybrid approach lets you write a single, self-contained file that handles both batch-style workflow (launching programs, waiting) and the keyboard simulation you need for your UAC bypass attempts.
内容的提问来源于stack exchange,提问作者dieeguds

