Force Batch File To Run As Admin Better -
But then—Server 47 hung.
The script was demanding interactive consent. On a headless server.
schtasks /create /tn "AdminElevate_%random%" /tr "%~dp0cleanup_logs.bat" /sc once /st 00:05 /rl highest /f schtasks /run /tn "AdminElevate_%random%" It worked. The task spawned, elevated silently, ran the script, and self-deleted. force batch file to run as admin
He pushed the final version to production at 12:17 AM.
He opened Notepad and loaded the batch file. At the very top, above the del commands and the rmdir sweeps, he typed: But then—Server 47 hung
He sighed. He could use a scheduled task with highest privileges. Or he could cheat.
Alan stared at the blinking cursor on his screen. It was 11:47 PM. The server migration was supposed to be done by 9. He opened Notepad and loaded the batch file
@echo off net session >nul 2>&1 if %errorlevel% neq 0 ( echo Requesting administrator privileges... powershell start -verb runas '%0' exit /b ) He saved it. Double-clicked.