Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have found the following routine which is supposed to allow a batch file to issue a UAC prompt and elevate itself to admin. Problem is on Server 2012 it doesn't seem to work for me. According to the notes I have read when a correct admin username/password is entered it should just continue with any code written beneath the below routine.

@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------
share|improve this question

1 Answer 1

I have not found any way to elevate batch files without the help of other programs. What i found is a tool which does the magic, because I do not like to do all the vbs-stuff.

I have successfully tested a call which might look like this

2>nul 1>nul start runasadmin.exe "%~dpf0"&&exit

This code calls it self with runasadmin.exe (i renamed it from elevate.exe) and if it was started it exits the non-elevated program.

Hope this helps on Server 2012!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.