I'm trying to force the user to run the script using administrative privileges:
@echo off
net session >nul 2>&1
if %errorlevel% == 0 (
echo Success: Administrative permissions confirmed.
echo.
echo Please Choose:
echo 1. Enable
echo 2. Disable
echo 3. Exit
set /p choice="> "
echo Choice: "%choice%"
) else (
echo Failure: Please run as administrator
)
pause
The net session
and errorlevel
lines check the privilege, and this works. The weird thing is that no matter what gets entered for choice
it behaves as if it was never initialized.
The code running inside the if block works fine by itself, so I suspect the issue has something to do with how I'm checking for privileges.
Can someone explain this behavior, and any fixes?