批处理之家's Archiver

/zhqsystem/zhq 发表于 2018-4-17 02:21

Bat权限控制运行支持三种运行方式

[i=s] 本帖最后由 /zhqsystem/zhq 于 2019-3-30 02:08 编辑 [/i]

转载请注明批处理之家
说明:
0=只运行普通权限
1=只运行管理员权限
其他值或不填=同时运行
系统要求NT6+,开启UAC有效,关闭UAC不需要这个

[code]call:UserAccountControlPermissionsApply 参数控制
goto:主标签或调用
goto:eof
:UserAccountControlPermissionsApply
rem:用户账户控制申请运行
cd/d "%~dp0"
if /i "%~1"=="0" echo,非管理员权限运行,包含管理员权限自动退出 字体:淡绿色&Color 0A
if /i "%~1"=="1" echo,保留管理员权限批处理运行,包含非管理员权限自动退出 字体:淡浅绿色&Color 0B
if /i not "%~1"=="0" if /i not "%~1"=="1" echo,保留管理员权限和非管理员权限同时运行.如果直接管理员运行无法调用普通权限自行创建计划任务 字体:黄色&&Color 0E
if /i "%~1"=="0" Reg.exe Query "HKU\S-1-5-20" >nul 2>nul&&exit
if /i not "%~1"=="0" if /i not "%~1"=="1" Reg.exe Query "HKU\S-1-5-20" >nul 2>nul&&goto:eof
if /i "%~1"=="1" Reg.exe Query "HKU\S-1-5-20" >nul 2>nul||"%systemroot%\system32\mshta.exe" vbscript:createobject("shell.application").shellexecute("""%cd%\%~nx0""",":UserAccountControlRequest",,"runas",1)(window.close)&&exit
if /i not "%~1"=="0" if /i not "%~1"=="1" Reg.exe Query "HKU\S-1-5-20" >nul 2>nul||"%systemroot%\system32\mshta.exe" vbscript:createobject("shell.application").shellexecute("""%cd%\%~nx0""",":UserAccountControlRequest",,"runas",1)(window.close)&&goto:eof
goto:eof[/code]

追加带有括号路径及其括号名称批处理代码管理员权限调用方法:2019/03/30 Update
[code]
@Echo Off&Color 0e
SetLocal EnableDelayedExpansion
Reg.exe Query "HKU\S-1-5-20" >nul 2>nul||(
>"%Temp%\TEMP.Bat" (
  Echo,@Echo Off^&Color 0e
  Echo,Reg.exe Query "HKU\S-1-5-20" ^>nul 2^>nul^|^|"%systemroot%\system32\mshta.exe" vbscript:createobject^("shell.application"^).shellexecute^("""%Temp%\Temp.Bat""","1",,"runas",1^)^(window.close^)^&^&exit
  Echo,Call "%~f0"
)
)
Reg.exe Query "HKU\S-1-5-20" >nul 2>nul||start "" "%Temp%\TEMP.Bat"
Reg.exe Query "HKU\S-1-5-20" >nul 2>nul||exit
Reg.exe Query "HKU\S-1-5-20" >nul 2>nul&&echo,管理员主命令
pause
结尾处需要删除临时Bat
goto:eof[/code]

hztccy 发表于 2021-8-11 12:21

好深奥,看不懂啊

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.