简体   繁体   English

创建一个.bat文件以管理员身份运行exe

[英]Create a .bat file to run an exe as administrator

I've created a batch file for running a setup.exe (code below) but I'm having issues getting the setup.exe to "run as administrator". 我已经创建了一个用于运行setup.exe的批处理文件(下面的代码)但是我遇到了使setup.exe“以管理员身份运行”的问题。 I used this guide (shortcut trick) for aid. 我使用指南(快捷方法)来获取帮助。

start "" %CD%\Setup.exe

NOTE: My files will eventually be burnt to a DVD disk. 注意:我的文件最终会被刻录到DVD磁盘上。 They are currently in the directory "C:\\Drivers\\win8.1_x64\\01a.chipset" . 它们目前位于"C:\\Drivers\\win8.1_x64\\01a.chipset"

The batch file code runs the setup.exe (without admin privileges) fine, when running the batch file by itself (ie not running the shortcut). 批处理文件代码在运行批处理文件时(即不运行快捷方式)运行setup.exe(没有管理员权限)。

However, when I run the batch file via the shortcut, Windows gives the error "Windows cannot find 'C:\\Windows\\system32\\Setup.exe'" . 但是,当我通过快捷方式运行批处理文件时,Windows会出现错误"Windows cannot find 'C:\\Windows\\system32\\Setup.exe'"

The setup.exe directory is not in the system32 folder. setup.exe目录不在system32文件夹中。 Why does running the batch file find the setup.exe fine but not when I run it by the shortcut (so I can run the setup.exe as an admin)? 为什么运行批处理文件发现setup.exe正常,但是当我通过快捷方式运行它时(因此我可以以管理员身份运行setup.exe)?

You can either set the working directory in the shortcut itself, or run Setup.exe not from working directory ( %CD% ) but from directory where script is located: 您可以在快捷方式本身中设置工作目录,也可以不从工作目录( %CD% )运行Setup.exe,而是从脚本所在的目录运行:

start "" "%~dp0\Setup.exe"

Difference from earlier answer is there's no need to cd to %~dp0 . 与之前的答案不同的是,不需要cd%~dp0 Just run the setup with full path. 只需使用完整路径运行安装程序。

By default admin privileged scripts are starting in C:\\Windows\\system32\\ 默认情况下,管理员特权脚本从C:\\Windows\\system32\\

Try to put cd /d "%~dp0" at the beginning of your script which should change the work directory to the script's one.You can check this if you want to create a shortcut with admin permissions from the command line. 尝试将cd /d "%~dp0"放在脚本的开头,这应该将工作目录更改为脚本的一个。如果要从命令行创建具有管理员权限的快捷方式,可以检查此项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM