Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Last active May 7, 2023 06:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidruhmann/4280586 to your computer and use it in GitHub Desktop.
Save davidruhmann/4280586 to your computer and use it in GitHub Desktop.
Check if user has Administrator privileges and request them if needed.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if Needed 'true'.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set "xVBUAC=%Temp%\AdminUAC.vbs"
set "xSuccess=true"
set "xAdmin=false"
if not "%~3"=="" set "xSuccess=%~3"
:: Check for Access
::net session >nul 2>&1
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set "xAdmin=%xSuccess%"
:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
if exist "%xVBUAC%" (
"%xVBUAC%"
rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
del "%xVBUAC%"
)
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof
:: by David Ruhmann
:: Example Admin check
:: by David Ruhmann
@echo off
call :Admin xReturn true 1
echo.%xReturn%
call :IsAdmin && echo Is Admin || echo Not Admin
goto End
:End
:: by David Ruhmann
If "%Version%"=="XP" GoTo SkipElevation
If "%Version%"=="2003" GoTo SkipElevation
PushD "%~dp0"
If Exist "%~0.ELEVATED" GoTo SkipElevation
:: Have to escape double quotes because they are passed to Cmd.exe via ShellExecute
Set CMD_Args=%0 %*
Set CMD_Args=%CMD_Args:"=\"%
Set ELEVATED_CMD=PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('Cmd.exe', '/C %CMD_Args%', '', 'runas')
Echo %ELEVATED_CMD% >> "%~0.ELEVATED"
:: If there are single quotes in the arguments, this will fail
Call %ELEVATED_CMD%
Exit
:SkipElevation
If Exist "%~0.ELEVATED" Del "%~0.ELEVATED"
:: ****************************************************************************
@echo off
echo Params = %*
call :IsAdminVerbose true || ( pause & exit /b )
pause
exit /b
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsAdminVerbose [Elevate|Params]
setlocal EnableExtensions
call :IsAdmin %*
set "Code=%ErrorLevel%"
if "%Code%" equ "0" echo Administrator: True
if "%Code%" equ "1" echo Administrator: False
endlocal & ( exit /b %Code% )
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsAdmin [Elevate|Params]
:: Check for Administrator privileges and request privileges if Elevate is set.
:: NOTE Elevate causes the batch script to restart and forget the arguments.
:: NOTE "runas" is an undocumented verb for the ShellExecute function.
:: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153.aspx
:: http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745.aspx
:: ErrorLevel 0 = Admin, 1 = Not Admin
setlocal EnableExtensions
:: Check for Access
::net session >nul 2>&1
"%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system" >nul 2>&1
if %ErrorLevel% EQU 0 ( exit /b 0 )
:: Check Preference
set "Elevate=%~1"
if not defined Elevate ( exit /b 1 )
:: Eleveate Privileges
call :ElevateVB %* || call :ElevatePS %*
endlocal & ( exit /b 1 )
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ElevateVB [Params]
:: ErrorLevel 0 = Success, 1 = File Error, 2 = Elevate Error
setlocal EnableExtensions
:: Generate VBScript
set "VB=%Temp%\Admin.vbs"
echo Set UAC = CreateObject^("Shell.Application"^) > "%VB%"
echo UAC.ShellExecute "%~s0", "%*", "", "runas", 1 >> "%VB%"
:: Execute VBScript
if not exist "%VB%" ( exit /b 1 )
"%VB%"
:: ERROR_ACCESS_DENIED 5 (0x5) Access is denied. (Most Common Reason for UAC)
::if %ErrorLevel% NEQ 5 ( echo Access is denied. )
del "%VB%"
endlocal & ( exit /b 0 )
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ElevatePS [Params]
:: ErrorLevel 0 = Success
setlocal EnableExtensions
:: Get Command Line and Escape Quotations
set "Args=%~s0 %*"
set "Args=%Args:"=\"%"
:: Execute PowerShell Command
PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('cmd.exe', '/k %Args%', '', 'runas')
endlocal & ( exit /b %ErrorLevel% )
@echo off
call :IsAdminVerbose true || ( exit /b )
exit /b
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsAdminVerbose [Prompt]
setlocal EnableExtensions
call :IsAdmin %1
set "Code=%ErrorLevel%"
if "%Code%" equ "0" echo Administrator: True
if "%Code%" equ "1" echo Administrator: False
if "%Code%" equ "2" echo Administrator: False [Error]
if "%Code%" equ "3" echo Administrator: False [Error]
endlocal & ( exit /b %Code% )
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsAdmin [Prompt]
:: Check for Administrator privileges and request privileges if Prompt is set.
:: NOTE that the Prompt causes the batch script to restart.
:: ErrorLevel 0 = Admin, 1 = Not Admin, 2 = Script Error, 3 = Prompt Error
setlocal EnableExtensions
:: Check for Access
::net session >nul 2>&1
"%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system" >nul 2>&1
if %ErrorLevel% EQU 0 ( exit /b 0 )
:: Check Preference
set "Prompt=%~1"
if not defined Prompt ( exit /b 1 )
:: Generate VB
set "VB=%Temp%\Admin.vbs"
echo Set UAC = CreateObject^("Shell.Application"^)>"%VB%"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1>>"%VB%"
:: Execute VB
if not exist "%VB%" ( exit /b 2 )
set "Code=1"
"%VB%"
:: ERROR_ACCESS_DENIED 5 (0x5) Access is denied.
if %ErrorLevel% NEQ 5 ( set "Code=3" )
del "%VB%"
endlocal & ( exit /b %Code% )
@davidruhmann
Copy link
Author

Recommend using the Hybrid.bat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment