How to create a batch file to run cmd as administrator

前端 未结 12 2118
一个人的身影
一个人的身影 2020-12-04 19:43

I need to run a batch file which needs to register a DLL. The DLL registration is failing because the Batch file is not starting the command prompt as \"administrator\".

相关标签:
12条回答
  • 2020-12-04 20:15

    To prevent the script from failing when the script file resides on a non system drive (c:) and in a directory with spaces.

    Batch_Script_Run_As_Admin.cmd

    @echo off
    if _%1_==_payload_  goto :payload
    
    :getadmin
        echo %~nx0: elevating self
        set vbs=%temp%\getadmin.vbs
        echo Set UAC = CreateObject^("Shell.Application"^)                >> "%vbs%"
        echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
    goto :eof
    
    :payload
    
    ::ENTER YOUR CODE BELOW::   
    
    
    
    
    
    ::END OF YOUR CODE::
    
    echo.
    echo...Script Complete....
    echo.
    
    pause
    
    0 讨论(0)
  • 2020-12-04 20:18

    (This is based on @DarkXphenomenon's answer, which unfortunately had some problems.)

    You need to enclose your code within this wrapper:

    if _%1_==_payload_  goto :payload
    
    :getadmin
        echo %~nx0: elevating self
        set vbs=%temp%\getadmin.vbs
        echo Set UAC = CreateObject^("Shell.Application"^)                >> "%vbs%"
        echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
    goto :eof
    
    :payload
        echo %~nx0: running payload with parameters:
        echo %*
        echo ---------------------------------------------------
        cd /d %2
        shift
        shift
        rem put your code here
        rem e.g.: perl myscript.pl %1 %2 %3 %4 %5 %6 %7 %8 %9
    goto :eof
    

    This makes batch file run itself as elevated user. It adds two parameters to the privileged code:

    • word payload, to indicate this is payload call, i.e. already elevated. Otherwise it would just open new processes over and over.

    • directory path where the main script was called. Due to the fact that Windows always starts elevated cmd.exe in "%windir%\system32", there's no easy way of knowing what the original path was (and retaining ability to copy your script around without touching code)

    Note: Unfortunately, for some reason shift does not work for %*, so if you need to pass actual arguments on, you will have to resort to the ugly notation I used in the example (%1 %2 %3 %4 %5 %6 %7 %8 %9), which also brings in the limit of maximum of 9 arguments

    0 讨论(0)
  • 2020-12-04 20:18

    Make a text using notepad or any text editor of you choice. Open notepad, write this short command "cmd.exe" without the quote aand save it as cmd.bat.

    Click cmd.bat and choose "run as administrator".

    0 讨论(0)
  • 2020-12-04 20:22

    As user2549366 suggested before, "You can use a shortcut that links to the batch file." but in the Properties->Compatibility tab of the shortcut, run as administrator may be disabled.

    So instead You just right click on your "file.bat - shortcut" then go to ->Properties->Shortcut tab -> Advanced and there you can click Run as administrator. After that, You can execute the shortcut.

    0 讨论(0)
  • 2020-12-04 20:24

    This Works for me`in Windows 7 to 10 with parameters and also kick starting app or file from any where(including browser) and also accessing file from anywhere,Replace (YOUR BATCH SCRIPT HERE anchor) with your code ,This solution May Help :)

         @echo off
    
         call :isAdmin
    
         if %errorlevel% == 0 (
            goto :run
         ) else (
            echo Requesting administrative privileges...
            goto :UACPrompt
         )
    
         exit /b
    
         :isAdmin
            fsutil dirty query %systemdrive% >nul
         exit /b
    
         :run
          <YOUR BATCH SCRIPT HERE>
         exit /b
    
         :UACPrompt
           echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
           echo UAC.ShellExecute "cmd.exe", "/c %~s0 %~1", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
           "%temp%\getadmin.vbs"
           del "%temp%\getadmin.vbs"
          exit /B`
    
    0 讨论(0)
  • 2020-12-04 20:26

    Here's a more simple version of essentially the same file.

    @echo off
    break off
    title C:\Windows\system32\cmd.exe
    cls
    
    :cmd
    set /p cmd=C:\Enter Command:
    
    %cmd%
    echo.
    goto cmd
    
    0 讨论(0)
提交回复
热议问题