如何创建一个批处理文件来作为管理员运行cmd

时间:2021-05-30 02:29:39

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".

我需要运行一个批处理文件,需要注册一个DLL。DLL注册失败是因为批处理文件没有启动命令提示符为“administrator”。

Is there any way to start the "Command Prompt" as administrator through the batch file.

是否有办法通过批处理文件以管理员的身份启动“命令提示符”。

Environment: Win7/Vista

环境:Win7 / Vista

12 个解决方案

#1


5  

You might have to use another batch file first to launch the second with admin rights.

您可能必须首先使用另一个批处理文件来启动第二个具有管理权限的文件。

In the first use

在第一次使用

runas /noprofile /user:mymachine\administrator yourbatchfile.bat

Upon further reading, you must be able to type in the password at the prompt. You cannot pipe the password as this feature was locked down for security reasons.

进一步阅读后,您必须能够在提示符处输入密码。由于出于安全原因,此特性被锁定,因此无法设置密码。

You may have more luck with psexec.

你在psexec可能会有更多的运气。

#2


22  

This script does the trick! Just paste it into the top of your bat file. If you want to review the output of your script, add a "pause" command at the bottom of your batch file.

这个脚本成功了!把它粘贴到你的bat文件的顶部。如果想查看脚本的输出,请在批处理文件的底部添加“pause”命令。

This script is now slightly edited to support command line args.

这个脚本现在稍微进行了编辑,以支持命令行args。

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

::ENTER YOUR CODE BELOW:

#3


7  

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

(这是基于@DarkXphenomenon的回答,不幸的是,这个回答有一些问题。)

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)

    主脚本被调用的目录路径。因为Windows总是会启动cmd。在“%windir%\system32”中,没有一种简单的方法可以知道原始路径是什么(以及在不接触代码的情况下保留复制脚本的能力)

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

注意:不幸的是,由于某些原因改变不为% *工作,所以如果你需要通过实际参数,你将不得不求助于丑陋的符号我使用的例子(% 1% 2% 3% 4% 5% 6% 7% 8% 9),也带来了最大的极限9参数

#4


3  

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 :)

这适用于Windows 7到10中的me ',带有参数,也可以从任何地方(包括浏览器)启动应用程序或文件,也可以从任何地方访问文件,用代码替换(这里的批处理脚本锚定),这个解决方案可能会有所帮助:)

     @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`

#5


3  

Press Ctrl+Shift and double-click a shortcut to run as an elevated process.

按Ctrl+Shift,双击快捷键作为一个提升的进程运行。

Works from the start menu as well.

也可以从开始菜单中工作。

#6


2  

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

当脚本文件位于非系统驱动器(c:)和具有空格的目录中时,防止脚本失败。

Batch_Script_Run_As_Admin.cmd

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

#7


1  

You can use a shortcut that links to the batch file. Just go into properties for the shortcut and select advanced, then "run as administrator".

可以使用链接到批处理文件的快捷方式。只需进入快捷方式的属性,然后选择advanced,然后“作为管理员运行”。

Then just make the batch file hidden, and run the shortcut.

然后将批处理文件隐藏起来,并运行快捷方式。

This way, you can even set your own icon for the shortcut.

这样,您甚至可以为快捷方式设置自己的图标。

#8


1  

Maybe something like this:

也许是这样的:

if "%~s0"=="%~s1" ( cd %~sp1 & shift ) else (
  echo CreateObject^("Shell.Application"^).ShellExecute "%~s0","%~0 %*","","runas",1 >"%tmp%%~n0.vbs" & "%tmp%%~n0.vbs" & del /q "%tmp%%~n0.vbs" & goto :eof
)

#9


1  

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

#10


0  

this might be a solution, i have done something similar but this one does not seem to work for example if the necessary function requires administrator privileges it should ask you to restart it as admin.

这可能是一个解决方案,我已经做了一些类似的事情,但是这个似乎不能工作,例如,如果必要的功能需要管理员权限,它应该要求您重新启动它作为admin。

@echo off
mkdir C:\Users\cmdfolder

if echo=="Access is denied." (goto :1A) else (goto :A4)

:A1
cls 
color 0d
echo restart this program as administator

:A4
pause

#11


0  

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.

正如user2549366所建议的那样,“您可以使用一个链接到批处理文件的快捷方式”,但是在属性—快捷方式的>兼容性选项卡中,作为管理员运行可能会被禁用。

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.

所以你只需右键点击你的“文件”。然后转到->属性->快捷键选项卡->高级,在那里你可以点击运行作为管理员。之后,可以执行快捷方式。

#12


-3  

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.

使用记事本或你选择的任何文本编辑器创建一个文本。打开记事本,写下这个简短的命令“cmd”。没有引用,保存为cmd。bat。

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

单击cmd。bat并选择“作为管理员运行”。

#1


5  

You might have to use another batch file first to launch the second with admin rights.

您可能必须首先使用另一个批处理文件来启动第二个具有管理权限的文件。

In the first use

在第一次使用

runas /noprofile /user:mymachine\administrator yourbatchfile.bat

Upon further reading, you must be able to type in the password at the prompt. You cannot pipe the password as this feature was locked down for security reasons.

进一步阅读后,您必须能够在提示符处输入密码。由于出于安全原因,此特性被锁定,因此无法设置密码。

You may have more luck with psexec.

你在psexec可能会有更多的运气。

#2


22  

This script does the trick! Just paste it into the top of your bat file. If you want to review the output of your script, add a "pause" command at the bottom of your batch file.

这个脚本成功了!把它粘贴到你的bat文件的顶部。如果想查看脚本的输出,请在批处理文件的底部添加“pause”命令。

This script is now slightly edited to support command line args.

这个脚本现在稍微进行了编辑,以支持命令行args。

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

::ENTER YOUR CODE BELOW:

#3


7  

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

(这是基于@DarkXphenomenon的回答,不幸的是,这个回答有一些问题。)

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)

    主脚本被调用的目录路径。因为Windows总是会启动cmd。在“%windir%\system32”中,没有一种简单的方法可以知道原始路径是什么(以及在不接触代码的情况下保留复制脚本的能力)

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

注意:不幸的是,由于某些原因改变不为% *工作,所以如果你需要通过实际参数,你将不得不求助于丑陋的符号我使用的例子(% 1% 2% 3% 4% 5% 6% 7% 8% 9),也带来了最大的极限9参数

#4


3  

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 :)

这适用于Windows 7到10中的me ',带有参数,也可以从任何地方(包括浏览器)启动应用程序或文件,也可以从任何地方访问文件,用代码替换(这里的批处理脚本锚定),这个解决方案可能会有所帮助:)

     @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`

#5


3  

Press Ctrl+Shift and double-click a shortcut to run as an elevated process.

按Ctrl+Shift,双击快捷键作为一个提升的进程运行。

Works from the start menu as well.

也可以从开始菜单中工作。

#6


2  

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

当脚本文件位于非系统驱动器(c:)和具有空格的目录中时,防止脚本失败。

Batch_Script_Run_As_Admin.cmd

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

#7


1  

You can use a shortcut that links to the batch file. Just go into properties for the shortcut and select advanced, then "run as administrator".

可以使用链接到批处理文件的快捷方式。只需进入快捷方式的属性,然后选择advanced,然后“作为管理员运行”。

Then just make the batch file hidden, and run the shortcut.

然后将批处理文件隐藏起来,并运行快捷方式。

This way, you can even set your own icon for the shortcut.

这样,您甚至可以为快捷方式设置自己的图标。

#8


1  

Maybe something like this:

也许是这样的:

if "%~s0"=="%~s1" ( cd %~sp1 & shift ) else (
  echo CreateObject^("Shell.Application"^).ShellExecute "%~s0","%~0 %*","","runas",1 >"%tmp%%~n0.vbs" & "%tmp%%~n0.vbs" & del /q "%tmp%%~n0.vbs" & goto :eof
)

#9


1  

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

#10


0  

this might be a solution, i have done something similar but this one does not seem to work for example if the necessary function requires administrator privileges it should ask you to restart it as admin.

这可能是一个解决方案,我已经做了一些类似的事情,但是这个似乎不能工作,例如,如果必要的功能需要管理员权限,它应该要求您重新启动它作为admin。

@echo off
mkdir C:\Users\cmdfolder

if echo=="Access is denied." (goto :1A) else (goto :A4)

:A1
cls 
color 0d
echo restart this program as administator

:A4
pause

#11


0  

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.

正如user2549366所建议的那样,“您可以使用一个链接到批处理文件的快捷方式”,但是在属性—快捷方式的>兼容性选项卡中,作为管理员运行可能会被禁用。

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.

所以你只需右键点击你的“文件”。然后转到->属性->快捷键选项卡->高级,在那里你可以点击运行作为管理员。之后,可以执行快捷方式。

#12


-3  

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.

使用记事本或你选择的任何文本编辑器创建一个文本。打开记事本,写下这个简短的命令“cmd”。没有引用,保存为cmd。bat。

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

单击cmd。bat并选择“作为管理员运行”。