如何自动提升我的批处理文件,以便在需要时请求 UAC 管理员权限? [英] How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

查看:23
本文介绍了如何自动提升我的批处理文件,以便在需要时请求 UAC 管理员权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的批处理文件只运行提升.如果未提升,请为用户提供一个选项以重新启动批处理作为提升.

I want my batch file to only run elevated. If not elevated, provide an option for the user to relaunch batch as elevated.

我正在编写一个批处理文件来设置系统变量,将两个文件复制到 Program Files 位置,然后启动驱动程序安装程序.如果 Windows 7/Windows Vista 用户(UAC 启用,即使他们是本地管理员) 运行它而无需右键单击并选择以管理员身份运行",他们将得到拒绝访问"复制两个文件并写入系统变量.

I'm writing a batch file to set a system variable, copy two files to a Program Files location, and start a driver installer. If a Windows 7/Windows Vista user (UAC enabled and even if they are a local admin) runs it without right-clicking and selecting "Run as Administrator", they will get 'Access Denied' copying the two files and writing the system variable.

如果用户实际上是管理员,我想使用命令以提升权限自动重新启动批处理.否则,如果他们不是管理员,我想告诉他们他们需要管理员权限才能运行批处理文件.我正在使用 xcopy 来复制文件并使用 REG ADD 来编写系统变量.我正在使用这些命令来处理可能的 Windows XP 机器.我发现了关于这个主题的类似问题,但没有任何关于重新启动批处理文件作为提升的问题.

I would like to use a command to automatically restart the batch as elevated if the user is in fact an administrator. Otherwise, if they are not an administrator, I want to tell them that they need administrator privileges to run the batch file. I'm using xcopy to copy the files and REG ADD to write the system variable. I'm using those commands to deal with possible Windows XP machines. I've found similar questions on this topic, but nothing that deals with relaunching a batch file as elevated.

推荐答案

您可以使用 psexec-h 选项运行提升.

You can have the script call itself with psexec's -h option to run elevated.

我不确定您将如何检测它是否已经以提升的方式运行...也许只有在出现拒绝访问错误时才使用提升的权限重试?

I'm not sure how you would detect if it's already running as elevated or not... maybe re-try with elevated perms only if there's an Access Denied error?

或者,您可以简单地让 xcopyreg.exe 的命令始终使用 psexec -h 运行,但它会如果最终用户每次都需要输入密码(或者如果您在脚本中包含密码则不安全),这对最终用户来说很烦人...

Or, you could simply have the commands for the xcopy and reg.exe always be run with psexec -h, but it would be annoying for the end-user if they need to input their password each time (or insecure if you included the password in the script)...

这篇关于如何自动提升我的批处理文件,以便在需要时请求 UAC 管理员权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆