0

I've got 2 separate batch files, the first one checks whether UAC is enabled or disabled. If enabled it calls "uac.bat".

REG QUERY HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ /v EnableLUA | FIND "0x1" >NUL && ( call uac.bat ) || ( echo Good day sir )

"uac.bat" that is located in the same folder as the previous batch file, pops-up the UAC prompt:

@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"
:--------------------------------------

I've tried using "call" command, but I'm doing something wrong because even if my UAC is disabled it still pop's up the UAC prompt as if my UAC were enabled. How to fix this?

Thanks in forward :)

Perulo
  • 69
  • 2
  • 7
  • 2
    UAC will be popping up because you are requesting it as the batch file doesn't have admin rights? Try logging on as admin and running it. – Bali C Jul 02 '12 at 10:36
  • The script posted above works perfectly. It's just that it's in 2 separate batch files. I need it to be in 1. Thanks in advance! :) – Perulo Jul 03 '12 at 13:01
  • Thats not what the question says :) In that case could you not put the `reg query` line at the top of the other one and call a label to the UAC part? – Bali C Jul 03 '12 at 13:12
  • I'm sorry, not sure if I understand:/ What the batch file is supposed to do is 1. check whether UAC is enabled or disabled 2. if disabled, does nothing 3. if enabled, pops up the UAC prompt It's just that I'm trying to get it to do so in 1 batch file, instead of 2 – Perulo Jul 03 '12 at 16:30
  • Ok, see my answer, that should do it :) – Bali C Jul 04 '12 at 08:28

1 Answers1

0

This should merge the two into one.

@echo off
REG QUERY HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ /v EnableLUA | FIND "0x1" >NUL && ( goto :PROMPTUAC )
exit >nul

:PROMPTUAC
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
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"
Bali C
  • 30,582
  • 35
  • 123
  • 152