windows 如何编写 BAT 文件以始终以管理员模式运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6811372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:36:32  来源:igfitidea点击:

How to code a BAT file to always run as admin mode?

windowsbatch-file

提问by karikari

I have this line inside my BAT file:

我的 BAT 文件中有这一行:

"Example1Server.exe"

I would like to execute this in Administrator mode. How to modify the bat code to run this as admin?

我想在管理员模式下执行此操作。如何修改 bat 代码以管理员身份运行它?

Is this correct? Do I need to put the quotes?

这样对吗?我需要加引号吗?

runas /user:Administrator invis.vbs Example1Server.exe

采纳答案by Kerrek SB

You use runasto launch a program as a specific user:

您用于runas以特定用户身份启动程序:

runas /user:Administrator Example1Server.exe

回答by Ed Greaves

The other answer requires that you enter the Administrator account password. Also, running under an account in the Administrator Group is not the same as run as administratorsee: UAC on Wikipedia

另一个答案要求您输入管理员帐户密码。此外,在管理员组中的帐户下运行与以管理员身份运行不同,请参阅:维基百科上的 UAC

Windows 7 Instructions

Windows 7 说明

In order to run as an Administrator, create a shortcut for the batch file.

为了以管理员身份运行,请为批处理文件创建快捷方式。

  1. Right click the batch file and click copy
  2. Navigate to where you want the shortcut
  3. Right click the background of the directory
  4. Select Paste Shortcut
  1. 右键单击批处理文件,然后单击复制
  2. 导航到您想要快捷方式的位置
  3. 右键单击目录的背景
  4. 选择粘贴快捷方式

Then you can set the shortcut to run as administrator:

然后你可以设置快捷方式以管理员身份运行:

  1. Right click the shortcut
  2. Choose Properties
  3. In the Shortcut tab, click Advanced
  4. Select the checkbox "Run as administrator"
  5. Click OK, OK
  1. 右键单击快捷方式
  2. 选择属性
  3. 在快捷方式选项卡中,单击高级
  4. 选中“以管理员身份运行”复选框
  5. 点击确定,确定

Now when you double click the shortcut it will prompt you for UAC confirmation and then Run as administrator(which as I said above is different than running under an account in the Administrator Group)

现在,当您双击快捷方式时,它会提示您进行 UAC 确认,然后以管理员身份运行(正如我上面所说,这与在管理员组中的帐户下运行不同)

Check the screenshot below

检查下面的屏幕截图

Screenshot

截屏

Note:When you do so to Run As Administrator, the current directory (path) will not be same as the bat file. This can cause some problems in many cases that the bat file refer to relative files beside it. For example, in my Windows 7 the cur dir will be SYSTEM32 instead of bat file location! To workaround it, you should use

注意:当您以管理员身份运行时,当前目录(路径)将与 bat 文件不同。这在很多情况下会导致一些问题,即bat文件引用了它旁边的相关文件。例如,在我的 Windows 7 中,cur 目录将是 SYSTEM32 而不是 bat 文件位置! 要解决它,您应该使用

cd "%~dp0"

cd "%~dp0"

or better

或更好

pushd "%~dp0"

pushd "%~dp0"

to ensure cur dir is at the same path where the bat file is.

确保 cur 目录与 bat 文件所在的路径相同。

回答by Sire

Just add this to the top of your bat file:

只需将此添加到您的 bat 文件的顶部:

set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )

It will elevate to admin and also stay in the correct directory. Tested on Windows 10.

它将提升为管理员并保留在正确的目录中。在 Windows 10 上测试。

回答by aphoria

If you can use a third party utility, here is an elevatecommand line utility.

如果您可以使用第三方实用程序,这里有一个elevate命令行实用程序

This is the usage description:

这是使用说明:

Usage: Elevate [-?|-wait|-k] prog [args]
-?    - Shows this help
-wait - Waits until prog terminates
-k    - Starts the the %COMSPEC% environment variable value and
                executes prog in it (CMD.EXE, 4NT.EXE, etc.)
prog  - The program to execute
args  - Optional command line arguments to prog

回答by Dheeraj Bhaskar

You can use nircmd.exe's elevatecommand

您可以使用nircmd.exe迅速提升命令

NirCmd Command Reference - elevate

NirCmd 命令参考 - 提升

elevate [Program] {Command-Line Parameters}

For Windows Vista/7/2008 only: Run a program with administrator rights. When the [Program] contains one or more space characters, you must put it in quotes.

仅适用于 Windows Vista/7/2008:以管理员权限运行程序。当 [Program] 包含一个或多个空格字符时,您必须将其放在引号中。

Examples:

例子:

elevate notepad.exe 
elevate notepad.exe C:\Windows\System32\Drivers\etc\HOSTS 
elevate "c:\program files\my software\abc.exe"

PS: I use it on win 10 and it works

PS:我在 win 10 上使用它并且它有效

回答by mattn

go get github.com/mattn/sudo

Then

然后

sudo Example1Server.exe

回答by Anonymous

I think I have a solution to the password problem. This single argument is truly amazing. It asks for the password once, and than never asks for it again. Even if you put it onto another program, it will not ask for the password. Here it is:

我想我有密码问题的解决方案。这个单一的论点确实令人惊叹。它只要求输入一次密码,然后再也不要求输入。即使你把它放到另一个程序上,它也不会要求输入密码。这里是:

runas /user:Administrator /savecred Example1Server.exe

runas /user:Administrator /savecred Example1Server.exe

回答by xxedxx

convert your batch file into .exe with this tool: http://www.battoexeconverter.com/then you can run it as administrator

使用此工具将您的批处理文件转换为 .exe:http: //www.battoexeconverter.com/然后您可以以管理员身份运行它

回答by pstraton

  1. My experimenting indicates that the runascommand must include the admin user's domain (at least it does in my organization's environmental setup):

    runas /user:AdminDomain\AdminUserName ExampleScript.bat
    

    If you don't already know the admin user's domain, run an instance of Command Prompt as the admin user, and enter the following command:

    echo %userdomain%
    
  2. The answers provided by both Kerrek SBand Ed Greaveswill execute the target file under the admin user but, if the file is a Command script (.bat file) or VB script (.vbs file) which attempts to operate on the normal-login user's environment (such as changing registry entries), you may not get the desired results because the environment under which the script actually runs will be that of the admin user, not the normal-login user! For example, if the file is a script that operates on the registry's HKEY_CURRENT_USER hive, the affected “current-user” will be the admin user, not the normal-login user.

  1. 我的实验表明runas命令必须包含管理员用户的域(至少在我组织的环境设置中是这样):

    runas /user:AdminDomain\AdminUserName ExampleScript.bat
    

    如果您还不知道管理员用户的域,请以管理员用户身份运行命令提示符实例,然后输入以下命令:

    echo %userdomain%
    
  2. Kerrek SBEd Greaves提供的答案将在 admin 用户下执行目标文件,但是,如果文件是命令脚本(.bat 文件)或 VB 脚本(.vbs 文件),它试图在正常登录上运行用户的环境(例如更改注册表项),您可能无法获得预期的结果,因为脚本实际运行的环境将是 admin 用户的环境,而不是普通登录用户的环境!例如,如果文件是在注册表的 HKEY_CURRENT_USER 配置单元上运行的脚本,则受影响的“当前用户”将是管理员用户,而不是普通登录用户。

回答by Samiamias

Use the complete physical drive\path to your Target batch file in the shortcut Properties.

在快捷方式属性中使用完整的物理驱动器\路径到目标批处理文件。

This does not work in Windows 10 if you use subst drives like I tried to do at first...

如果您像我最初尝试的那样使用 subst 驱动器,这在 Windows 10 中不起作用...