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

其他回答

另一种方法是

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

然后

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

基于toster-cx的帖子和本页上其他有趣的帖子,我对如何配置和解决我的问题有了深入的了解。我也遇到过类似的问题,我希望磁盘清理工具每周在周一和周四的午餐时间(比如下午2点)运行两次。然而,这需要更高的权利。

共享批处理文件,这可能会帮助其他初学者像我-

@echo off
echo  Welcome to scheduling 'PC Maintenance Activity'
ping localhost -n 3 >nul
echo -- Step - 1 of 3 : Please give 'Admin' rights on next screen
ping localhost -n 5 >nul
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit)
cls
echo -- Step - 2 of 3 : In next screen, select temp areas for cleaning 
during routine scheduled activity
ping localhost -n 3 >nul
C:\Windows\System32\cleanmgr.exe /sageset:112
cls
echo    Now scheduling maintenance activity...
SchTasks /Create /SC WEEKLY /D MON,THU /TN PC_Cleanup /TR 
"C:\Windows\System32\cleanmgr.exe "/sagerun:112 /ST 14:00

cls

echo                         -- Thanks for your co-operation --
echo                    -- Maintenance activity is scheduled for --
echo                       -- Every Monday and Thursday at 2 pm --

ping localhost -n 10 >nul

非常感谢这个论坛和Rems POST在这里[https://www.petri.com/forums/forum/windows-scripting/general-scripting/32313-schtasks-exe-need-to-pass-parameters-to-script][1]

他的帖子有助于在调度任务时配置可选参数。

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

我知道这不是OP的解决方案,但是因为我确信这里还有许多其他用例,所以我想分享一下。

我对这些答案中的所有代码示例都有问题,但后来我发现: http://www.robotronic.de/runasspcEn.html

它不仅允许您以管理员身份运行,还可以检查文件以确保它没有被篡改,并安全地存储所需的信息。我承认它不是最明显的工具,但对于我们这些编写代码的人来说,它应该足够简单。

使用runas命令。但是,我不认为您可以轻松地通过电子邮件发送.bat文件。