如何在批处理文件中请求管理员访问权限
我正在尝试为我的用户编写一个批处理文件,以便使用 UAC 从他们的 Vista 计算机上运行。该文件正在重写其主机文件,因此需要以管理员权限运行。我需要能够向他们发送一封包含 .bat 文件链接的电子邮件。期望的行为是,当他们右键单击文件并说“打开”时,他们将看到 UAC 对话框之一,该对话框使屏幕变暗并迫使他们回答是否要授予应用程序以管理员身份运行的权限。相反,他们只是在命令行窗口上看到“访问被拒绝”。
这可以采取不同的做法吗?
I am trying to write a batch file for my users to run from their Vista machines with UAC. The file is re-writing their hosts file, so it needs to be run with Administrator permissions. I need to be able to send them an email with a link to the .bat file. The desired behavior is that when they right-click on the file and say Open, they will get one of those UAC dialogs that makes the screen go dark and forces them to answer whether they want to give the application permission to run as administrator. Instead, they are just seeing "Access denied" on the command line window.
Is this possible to do differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
这个脚本就可以解决这个问题!只需将其粘贴到 bat 文件的顶部即可。如果您想查看脚本的输出,请在批处理文件的底部添加“暂停”命令。
更新:此脚本现已稍微编辑以支持命令行参数和 64 位操作系统。
谢谢 Eneerge @ https://sites.google.com/site/eneerge/scripts/batchgotadmin
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.
UPDATE: This script is now slightly edited to support command line arguments and a 64 bit OS.
Thank you Eneerge @ https://sites.google.com/site/eneerge/scripts/batchgotadmin
这是我一直在使用的:
注意:
-Verb RunAs
标志可启用管理提升。-ArgumentList
添加更多内容。请注意-ArgumentList
接受单个字符串或字符串数组。Here's what I've been using:
Notes:
-Verb RunAs
flag ofStart-Process
is what enables the administrative elevation.-ArgumentList
. Note that-ArgumentList
accepts either a single string or a string array.这是我的代码!它看起来很大,但主要是注释行(以 :: 开头的行)。
特点:
完整参数转发
不更改工作文件夹
错误处理
接受带括号的路径(%TEMP% 文件夹除外)
支持 UNC 路径
映射文件夹检查(如果管理员无法访问映射驱动器,则会向您发出警告)
可以用作外部库(查看我在该主题中的帖子:https://stackoverflow.com/a/30417025/4932683)
可以在代码中的任何地方需要时/如果需要时调用
只需将其附加到批处理文件的末尾,或将其另存为库(检查上面)
如何使用它的示例
Here is my code! It looks big but it is mostly comment lines (the lines starting with ::).
Features:
Full argument forwarding
Does not change working folder
Error handling
Accepts paths with parenthesis (except for %TEMP% folder)
Supports UNC paths
Mapped folder check (Warn´s you if admin can´t access mapped drive)
Can be used as an external library (check my post at this topic: https://stackoverflow.com/a/30417025/4932683)
Can be called when/if needed anywhere in your code
Just attach this to the end of your batch file, or save it as a library (check above)
Example on how to use it
另一种方法是在
,然后
Another approach is to
and then
Ben Gripka 的解决方案会导致无限循环。他的批处理工作如下(伪代码):
如您所见,如果 VBS 请求管理员权限失败,这会导致无限循环。
但是,尽管已成功请求管理员权限,但仍可能发生无限循环。
Ben Gripka 的批处理文件中的检查很容易出错。我尝试了该批处理,发现尽管检查失败,但管理员权限仍然可用。有趣的是,如果我从 Windows 资源管理器启动批处理文件,则检查按预期工作,但当我从 IDE 启动它时,检查却没有。
所以我建议使用两个单独的批处理文件。第一个生成调用第二个批处理文件的 VBS:
第二个名为“my_commands.bat”,位于与第一个批处理文件相同的目录中,包含您的实际命令:
这不会导致无限循环,并且还删除了容易出错的管理员权限检查。
Ben Gripka's solution causes infinite loops. His batch works like this (pseudo code):
As you can see, this causes an infinite loop, if the VBS fails requesting admin privileges.
However, the infinite loop can occur, although admin priviliges have been requested successfully.
The check in Ben Gripka's batch file is just error-prone. I played around with the batch and observed that admin privileges are available although the check failed. Interestingly, the check worked as expected, if I started the batch file from windows explorer, but it didn't when I started it from my IDE.
So I suggest to use two separate batch files. The first generates the VBS that calls the second batch file:
The second, named "my_commands.bat" and located in the same directory as the first contains your actual commands:
This causes no infinite loops and also removes the error-prone admin privilege check.
另一个PowerShell解决方案...
这不是关于以管理员身份运行批处理脚本,而是如何从批处理中提升另一个程序...
我有一个exe的批处理文件“包装器”。它们具有相同的“根文件名”,但扩展名不同。我能够以管理员身份启动 exe,并将工作目录设置为包含脚本的目录,使用以下一行 powershell 调用:
更多信息
还有一大堆额外的
Start - 处理您可以应用的选项!查看:https: //learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6
请注意,我使用
@
前缀。这相当于单行的@echo off
。我在这里使用%~n0
来获取批处理脚本的“根名称”,然后连接.exe
将其指向相邻的二进制文件。使用%~dp0
提供批处理所在目录的完整路径。当然,-Verb RunAs
参数提供海拔。Another PowerShell Solution...
This is not about running a batch script as admin per, but rather how to elevate another program from batch...
I have a batch file "wrapper" for an exe. They have the same "root file name", but alternate extensions. I am able to launch the exe as admin, and set the working directory to the one containing the script, with the following one line powershell invocation:
More info
There are a whole slew of additional
Start-Process
options as well that you can apply! Check out: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6Note that I use the
@
prefix. That's equivalent to@echo off
for the one line. I use%~n0
here to get the "root name" of the batch script, then I concatenate the.exe
to point it the adjancent binary. The use of%~dp0
provides the full path to the directory which the batch resides. And, of course, the-Verb RunAs
parameter provides the elevation.以上适用于我的 Windows 10 版本 1903
The above works on my Windows 10 Version 1903
我知道这不是 OP 的解决方案,但由于我确信这里还有许多其他用例,所以我想我会分享。
我对这些答案中的所有代码示例都遇到了问题,但后来我发现:
http://www.robotronic.de/runasspcEn.html
它不仅允许您运行admin,它检查文件以确保它没有被篡改并安全地存储所需的信息。我承认它不是最明显的了解如何使用的工具,但对于我们这些编写代码的人来说它应该足够简单。
I know this is not a solution for OP, but since I'm sure there are many other use cases here, I thought I would share.
I've had problems with all the code examples in these answers but then I found :
http://www.robotronic.de/runasspcEn.html
It not only allows you to run as admin, it checks the file to make sure it has not been tampered with and stores the needed information securely. I'll admit it's not the most obvious tool to figure out how to use but for those of us writing code it should be simple enough.
@echo off
和title
可以出现在这段代码之前:如果您不需要担心以下问题,那么许多其他答案都是多余的:
@echo off
andtitle
can come before this code:A lot of the other answers are overkill if you don't need to worry about the following:
cd %~dp0
will change to the directory containing the batch file)还有来自 这篇文章 的 FSUTIL 查询,该查询也链接在 ss64.com 具有以下代码:
只要 FSUTIL 存在,它就是一个可靠的替代方案。
There's also the FSUTIL query from this post which is also linked at ss64.com that has the following code:
As long as FSUTIL is around, it's a reliable alternative.
使用
mshta
提示输入管理员权限:或者使用 powershell:
Use
mshta
to prompt for admin rights:Or, using powershell:
由于我在无限循环(使用 Win 7 Pro)中遇到此脚本弹出新命令提示符并再次运行的问题,我建议您尝试另一种方法:如何自动提升我的批处理文件,以便它从如果需要 UAC 管理员权限?
请注意,您必须将其添加到脚本末尾,如编辑中所述,以便在权限提升后返回到脚本目录:
cd /d %~dp0
Since I have troubles with this script popping up a new command prompt with itself run again, in infinite loop (using Win 7 Pro), I suggest you try another approach :How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
Be careful, you have to add this at the end of script, as stated in an edit, so that you are back to script directory after privileges were elevated :
cd /d %~dp0
根据 toster-cx 的帖子和本页面上其他有趣的帖子,我深入了解了如何配置和解决我的问题。我遇到了类似的问题,我希望磁盘清理实用程序每周在周一和周四的午餐时间(比如下午 2 点)运行两次。然而,这需要更高的权利。
共享批处理文件,这可能会帮助像我这样的其他初学者 -
非常感谢这个论坛和 Rems 在这里发帖 [https://www.petri.com/forums/forum/windows-scripting/general-scripting/ 32313-schtasks-exe-need-to-pass-parameters-to-script][1]
他的帖子有助于在安排任务时配置可选参数。
Based on post by toster-cx and other interesting posts on this page, I got insight on how to configure and solve my problem. I had similar issue where I wished that Disk Cleanup utility runs every week twice on Monday and Thursday during lunch hours (say 2 pm). However, this required elevated rights.
Sharing batch file which might help other beginners like me -
Thanks a lot for this forum and Rems POST here [https://www.petri.com/forums/forum/windows-scripting/general-scripting/32313-schtasks-exe-need-to-pass-parameters-to-script][1]
His post helped for configuring optional argument while scheduling the task.
您无法从批处理文件请求管理员权限,但您可以在 %temp% 中编写一个 Windows 脚本主机脚本并运行该脚本(进而以管理员身份执行您的批处理)您想要在 Shell 中调用 ShellExecute 方法。以“runas”作为动词的应用程序对象
You can't request admin rights from a batch file, but you could write a windows scripting host script in %temp% and run that (and that in turn executes your batch as admin) You want to call the ShellExecute method in the Shell.Application object with "runas" as the verb
我使用了多个示例来将这个工作衬里修补在一起。
这将以
ADMIN + 最大化窗口
的形式打开批处理脚本,只需将以下代码之一添加到批处理脚本的顶部即可。
两种方法都可以,只是编码方式不同。
我相信第一个示例响应最快,因为
/d
开关禁用了我已启用的 doskey 命令。示例一
示例二
请参阅下文使用原始批处理代码时的建议
将原始批处理代码
完整放置
只是因为最顶部的第一行代码有
@ECHO OFF
并不意味着如果您的原始脚本不应该再次包含它
也有。
这确保了当脚本在现在在管理中运行的新窗口中重新启动时
您不会丢失预期的脚本参数/属性的模式...
例如当前工作目录、本地变量等
您可以从以下命令开始以避免其中一些问题
I used multiple examples to patch this working one liner together.
This will open your batch script as an
ADMIN + Maximized Window
Just add one of the following codes to the top of your batch script.
Both ways work, just different ways to code it.
I believe the first example responds the quickest due to
/d
switch disabling my doskey commands that I have enabled..EXAMPLE ONE
EXAMPLE TWO
See below for recommendations when using your original batch code
Place the original batch code
in it's entirety
Just because the first line of code at the very top has
@ECHO OFF
doesn't mean you should not include it again if your original script
has it as well.
This ensures that when the script get's restarted in a new window now running in admin
mode that you don't lose your intended script parameters/attributes...
Such as the current working directory, your local variables, and so on
You could beginning with the following commands to avoid some of these issues
少&快速
LESS & FAST
使用 runas 命令。但是,我认为您无法轻松通过电子邮件发送 .bat 文件。
use the runas command. But, I don't think you can email a .bat file easily.