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.
有可能采取不同的做法吗?
还有来自这篇文章的FSUTIL查询,也在ss64.com上有链接,有以下代码:
@Echo Off
Setlocal
:: First check if we are running As Admin/Elevated
FSUTIL dirty query %SystemDrive% >nul
if %errorlevel% EQU 0 goto START
::Create and run a temporary VBScript to elevate this batch file
Set _batchFile=%~f0
Set _Args=%*
:: double up any quotes
Set _batchFile=""%_batchFile:"=%""
Set _Args=%_Args:"=""%
Echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\~ElevateMe.vbs"
Echo UAC.ShellExecute "cmd", "/c ""%_batchFile% %_Args%""", "", "runas", 1 >> "%temp%\~ElevateMe.vbs"
cscript "%temp%\~ElevateMe.vbs"
Exit /B
:START
:: set the current directory to the batch file location
cd /d %~dp0
:: Place the code which requires Admin/elevation below
Echo We are now running as admin [%1] [%2]
pause
只要FSUTIL存在,它就是一个可靠的替代方案。
我使用了多个例子来修补这个工作在一起的一行。
这将打开你的批处理脚本作为一个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参数提供抬高。