0

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"
:--------------------------------------
NickC
  • 2,373
  • 13
  • 41
  • 55

1 Answers1

0

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!

syss
  • 123
  • 7