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.

有可能采取不同的做法吗?


当前回答

你不能从批处理文件中请求管理权限,但是你可以在%temp%中编写一个windows脚本主机脚本并运行它(它反过来以管理员身份执行你的批处理)。使用“runas”作为动词的应用程序对象

其他回答

@echo off和title可以放在下面代码之前:

net session>nul 2>&1
if %errorlevel%==0 goto main
echo CreateObject("Shell.Application").ShellExecute "%~f0", "", "", "runas">"%temp%/elevate.vbs"
"%temp%/elevate.vbs"
del "%temp%/elevate.vbs"
exit

:main
    <code goes here>
exit

如果你不需要担心以下问题,那么很多其他答案都是多余的:

参数 工作目录(cd %~dp0将更改为包含批处理文件的目录)

我使用了多个例子来修补这个工作在一起的一行。

这将打开你的批处理脚本作为一个ADMIN +最大化窗口

只需将以下代码之一添加到批处理脚本的顶部。 两种方式都可以,只是编码方式不同。

我相信第一个例子响应最快,因为/d开关禁用了我已经启用的doskey命令。

一个例子

@ECHO OFF
IF NOT "%1"=="MAX" (powershell -WindowStyle Hidden -NoProfile -Command {Start-Process CMD -ArgumentList '/D,/C' -Verb RunAs} & START /MAX CMD /D /C %0 MAX & EXIT /B)
:--------------------------------------------------------------------------------------------------------------------------------------------------------------------

:: Your original batch code here:

:--------------------------------------------------------------------------------------------------------------------------------------------------------------------

两个例子

@ECHO OFF
IF NOT "%1"=="MAX" (powershell -WindowStyle Hidden -NoProfile -Command "Start-Process CMD -ArgumentList '/C' -Verb RunAs" & START /MAX CMD /C "%0" MAX & EXIT /B)
:--------------------------------------------------------------------------------------------------------------------------------------------------------------------

:: Your original batch code here:

:--------------------------------------------------------------------------------------------------------------------------------------------------------------------

在使用原始批处理代码时,请参阅下面的建议

完整地放置原始批处理代码

只是因为最上面的第一行代码有@ECHO OFF 但这并不意味着如果你的原始脚本不应该再包含它 也有。

这确保当脚本在一个新窗口中重新启动时,现在正在管理中运行 模式,您不会丢失预期的脚本参数/属性… 例如当前工作目录、本地变量等等

您可以从以下命令开始,以避免其中一些问题

:: Make sure to use @ECHO OFF if your original code had it
@ECHO OFF
:: Avoid clashing with other active windows variables with SETLOCAL
SETLOCAL
:: Nice color to work with using 0A
COLOR 0A
:: Give your script a name
TITLE NAME IT!

:: Ensure your working directory is set where you want it to be
:: the following code sets the working directory to the script directory folder
PUSHD "%~dp0"

THE REST OF YOUR SCRIPT HERE...

:: Signal the script is finished in the title bar
ECHO.
TITLE Done! NAME IT!
PAUSE
EXIT

另一个PowerShell解决方案…

这不是关于作为管理员运行批处理脚本,而是如何从批处理提升另一个程序…

我有一个批处理文件“包装”的exe。它们具有相同的“根文件名”,但扩展名不同。我能够启动exe作为管理员,并将工作目录设置为一个包含脚本,与以下一行powershell调用:

@powershell "Start-Process -FilePath '%~n0.exe' -WorkingDirectory '%~dp0' -Verb RunAs"

更多信息

还有一大堆额外的Start-Process选项,你也可以申请!查看:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6

注意,我使用了@前缀。这相当于一行的@echo off。我在这里使用%~n0来获得批处理脚本的“根名称”,然后连接.exe以指向相邻的二进制文件。%~dp0的使用提供了批处理所在目录的完整路径。当然,-Verb RunAs参数提供抬高。

以下是我一直在用的:

@echo off
if not "%1"=="am_admin" (
    powershell -Command "Start-Process -Verb RunAs -FilePath '%0' -ArgumentList 'am_admin'"
    exit /b
)

echo main code here
pause

注:

Start-Process的-Verb RunAs标志是启用管理提升的标志。 只在windows 7和10上测试过,你可能得把引号弄乱 目前不支持传递参数,但你可以向-ArgumentList中添加更多的东西。注意-ArgumentList接受单个字符串或字符串数组。

另一种方法是

在本地创建快捷方式并将其设置为调用管理员权限(属性,高级,以管理员身份运行)

然后

向用户发送快捷方式(或指向快捷方式的链接,而不是指向批处理文件本身的链接)。