pintcat

admin.bat - Give admin privileges in a batch script

Apr 19th, 2022 (edited)
1,943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 0.80 KB | None | 0 0
  1. @echo off
  2. :: BatchGotAdmin
  3. :-------------------------------------
  4. REM --> Check for permissions
  5. >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
  6.  
  7. REM --> If error we don't have admin.
  8. if '%errorlevel%' NEQ '0' (
  9.     echo Requesting administrative privileges...
  10.     goto UACPrompt
  11. ) else ( goto gotAdmin )
  12.  
  13. :UACPrompt
  14.     echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
  15.     set params = %*:"=""
  16.     echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
  17.  
  18.     "%temp%\getadmin.vbs"
  19.     del "%temp%\getadmin.vbs"
  20.     exit /B
  21.  
  22. :gotAdmin
  23.     pushd "%CD%"
  24.     CD /D "%~dp0"
  25. :--------------------------------------
  26. REM --> Now with admin permissions given, we can do all the naughty things from here...
  27.  
Add Comment
Please, Sign In to add comment