Running a batch file with administrator Privileges

I need an easy way to let the QA people run chef, they alreay have accounts that are local admins so I wanted a batch file that they can click on that will run chef with admin privileges

the chef documentations suggests using runas

runas /user:Administrator "cmd /C chef-client"

But I don’t want to give the administrator password to the users.

I found this stack overflow posting that worked like a charm


:: Automatically check & get admin rights
@echo off
ECHO =============================
ECHO Running Admin shell
ECHO =============================

if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

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" 
exit /B 

setlocal & pushd .

REM Run shell as admin (example) - put here code as you like
cmd /k

Just replace cmd /k with your code.

Leave a Reply

Your email address will not be published. Required fields are marked *