在运行时请求提升权限
以下批处理文件将弹出 UAC 提示,允许你接受批处理会话的提升管理员权限。将任务代码添加到批处理的:usercode
部分,以便它们以提升的权限运行。
@echo off
setlocal EnableDelayedExpansion
:: test and acquire admin rights
cd /d %~dp0 & echo/
if not "%1"=="UAC" (
>nul 2>&1 net file && echo Got admin rights || (echo No admin rights & ^
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute("%~snx0", 'UAC', '', 'runas', 1);close();"))
:: re-test admin rights
echo/ & >nul 2>&1 net file && (echo Got admin rights & echo/) || (echo No admin rights. Exiting... & goto :end)
:usercode
:: add your code here
echo Performing admin tasks
echo Hello >C:\test.txt
:end
timeout /t 5 >nul
exit /b