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.
有可能采取不同的做法吗?
我使用了多个例子来修补这个工作在一起的一行。
这将打开你的批处理脚本作为一个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
本·格里普卡的解决方案导致了无限循环。他的批处理是这样工作的(伪代码):
IF "no admin privileges?"
"write a VBS that calls this batch with admin privileges"
ELSE
"execute actual commands that require admin privileges"
正如你所看到的,如果VBS请求管理员权限失败,这将导致一个无限循环。
但是,尽管已经成功请求了管理权限,但仍可能发生无限循环。
本·格里普卡批处理文件中的检查很容易出错。我对批处理进行了研究,并观察到尽管检查失败,但管理权限可用。有趣的是,如果我从windows资源管理器启动批处理文件,检查按预期工作,但当我从我的IDE启动它时,它没有。
所以我建议使用两个单独的批处理文件。第一个生成VBS,调用第二个批处理文件:
@echo off
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~dp0\my_commands.bat"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
第二个文件名为“my_commands.bat”,与第一个文件位于同一目录,包含您的实际命令:
pushd "%CD%"
CD /D "%~dp0"
REM Your commands which require admin privileges here
这不会导致无限循环,也会删除容易出错的管理特权检查。