-1

The question is different from How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

since there will be some problems when passing arguments to a batch file which has the code of acquiring administrative privileges. After the privileges have been obtained, the arguments is lost and become undefined. I solve this problem by storing the arguments to a file first, please see detail in my answer.


Say the require batch file is run_as_admin.bat, By running run_as_admin.bat your_command your_params we can execute the command with administrative privileges (e.g. run_as_admin delete /path/to/system_file).

  1. To run parameters as command, run_params_as_cmd.bat

    %*
    pause
    
  2. To get administrative privileges, I find some code from http://larrynung.github.io/2014/01/01/batch-run-as-administrator/

  3. Then I tried the following code run_as_admin.bat:

    @echo off
    
    call:TryToRunAsAdmin
    
    if %ERRORLEVEL%==1 exit /B
    
    echo got administrative privileges...
    
    :: run parameters as command <<< Here '%*' seems not be executed as a command.
    %* 
    pause
    
    goto :eof
    
    :TryToRunAsAdmin
    
        set GetAdminScriptFile="%temp%\getadmin.vbs"
    
        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...
            call:UACPrompt
            set ERRORLEVEL=1
        ) else ( 
            if exist %GetAdminScriptFile% ( del %GetAdminScriptFile% )
            set ERRORLEVEL=0
        )
        goto :eof
    
    
    :UACPrompt
    
        echo Set UAC = CreateObject^("Shell.Application"^) > %GetAdminScriptFile%
        echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> %GetAdminScriptFile%
    
        call %GetAdminScriptFile%
        goto :eof
    
  4. To test that code, I tried some command which needs admin privileges

    • run_as_admin.bat reg add "HKLM\SOFTWARE\Classes\.php" /v PerceivedType /d text

    • run_as_admin.bat %SystemRoot%\system32\drivers\etc\hosts_old.txt

but these commands seem to be not executed but ignored as a string.

I guess the problem is related to the code, is there any other ways to write a batch file can run parameters as a command with administrative privileges?

Community
  • 1
  • 1
応振强
  • 266
  • 3
  • 12
  • Add it to `HKCU\SOFTWARE\Classes` which is where user ones go. It takes precedence over HKLM. –  Jul 14 '16 at 03:19
  • 2
    Be more specific about *nothing happened*. Do you mean **absolutely nothing happened*? In that case you forgot to hit Enter at the end of your command. Do you mean it just didn't change the registry? Do you mean it did nothing at all? Did it print any of the text it should be printing as it goes along (according to the code)? You need to be **clear and specific** about the problem you're having, and state the question clearly. It's hard to say *what's wrong?* (which is a crappy question, BTW) when you've not told us what the problem is that you're having. – Ken White Jul 14 '16 at 03:19
  • 1
    Possible duplicate of [How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?](http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator) – TessellatingHeckler Jul 14 '16 at 06:29
  • Or like https://stackoverflow.com/questions/27270944/how-do-i-execute-a-batch-file-in-administrator-mode?rq=1 – SDsolar Apr 26 '17 at 02:04

1 Answers1

0

The arguments passing to run_as_admin.bat will gone when requesting administrative privileges. So you need to store the arguments first.

Here is the solution:

    @echo off
    SET commandline=%*
    :: store it to file
    echo %commandline% > "__temp__.bat"

    :: 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"
        set params = %*:"=""
        echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

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

    :gotAdmin
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------

    :: execute command administrative privileges with and then delete the temp file
    call "__temp__.bat"
    del  "__temp__.bat"

    pause
応振强
  • 266
  • 3
  • 12