Created
September 17, 2015 08:12
-
-
Save mrpapercut/cb6486106f1486815b22 to your computer and use it in GitHub Desktop.
Batch script that runs code as admin when called as non-admin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
GOTO checkAdmin | |
:: Check if batch-file is run with admin privileges | |
:checkAdmin | |
FSUTIL dirty query %systemdrive% >nul 2>&1 | |
IF %errorLevel% == 1 ( | |
GOTO isNotAdmin | |
) ELSE ( | |
GOTO isAdmin | |
) | |
:isNotAdmin | |
:: If you want to run the code both as non-admin AND as admin, uncomment the following line: | |
:: CALL :codeToExecute | |
:: Create elevated process that runs this file again | |
SETLOCAL DisableDelayedExpansion | |
SET "batchPath=%~0" | |
SETLOCAL EnableDelayedExpansion | |
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" | |
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" | |
"%temp%\OEgetPrivileges.vbs" | |
EXIT /B | |
GOTO EOF | |
:isAdmin | |
CALL :codeToExecute | |
GOTO EOF | |
:codeToExecute | |
:: Here goes the code to execute | |
:EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment