Require administrator privileges in a script.
- Just paste the code below at the beginning of your script (preferably as high as possible).
whoami /groups | find "S-1-16-12288" > nul
if %ErrorLevel% neq 0 (
echo Set UAC = CreateObject^("Shell.Application"^) > "%Temp%\UAC %~n0.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%Temp%\UAC %~n0.vbs"
"%Temp%\UAC %~n0.vbs"
del "%Temp%\UAC %~n0.vbs"
exit
)
- You can also add texts below
if %ErrorLevel% neq 0 (
.
whoami /groups | find "S-1-16-12288" > nul
if %ErrorLevel% neq 0 (
+ echo Accept Administrator Privileges to continue.
echo Set UAC = CreateObject^("Shell.Application"^) > "%Temp%\UAC %~n0.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%Temp%\UAC %~n0.vbs"
"%Temp%\UAC %~n0.vbs"
del "%Temp%\UAC %~n0.vbs"
exit
)
- You can add
pushd "%~dp0"
to go back to the script folder.
whoami /groups | find "S-1-16-12288" > nul
if %ErrorLevel% neq 0 (
echo Set UAC = CreateObject^("Shell.Application"^) > "%Temp%\UAC %~n0.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%Temp%\UAC %~n0.vbs"
"%Temp%\UAC %~n0.vbs"
del "%Temp%\UAC %~n0.vbs"
exit
)
+ pushd "%~dp0"