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.
有可能采取不同的做法吗?
这个脚本很有用!只需将其粘贴到bat文件的顶部。如果您想查看脚本的输出,请在批处理文件的底部添加“pause”命令。
更新:这个脚本现在稍微编辑了一下,以支持命令行参数和64位操作系统。
感谢能源@ https://sites.google.com/site/eneerge/scripts/batchgotadmin
@echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
<YOUR BATCH SCRIPT HERE>
基于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]
他的帖子有助于在调度任务时配置可选参数。
本·格里普卡的解决方案导致了无限循环。他的批处理是这样工作的(伪代码):
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
这不会导致无限循环,也会删除容易出错的管理特权检查。