bat默认以管理员运行
bat默认以管理员运行
当编写bat脚本时大部分需要用到管理员权限运行,通过右键菜单方式觉得有些麻烦,网上找了找,通过在脚本加入提权的脚本就可。
在bat 脚本前面加入以下内容
- 方式一
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof
:Admin
- 方式二
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
- 适用于无参数
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c "^&chr(34)^&"%~s0"^&chr(34)^&" ::","%cd%","runas",1)(window.close)&&exit
cd /d "%~dp0"
- 适用于一参数
%2 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c "^&chr(34)^&"%~s0"^&chr(34)^&" "^&chr(34)^&"%~s1"^&chr(34)^&" ::","%cd%","runas",1)(window.close)&&exit
cd /d "%~dp0"
更多参数的以此类推,运行批处理时多加一个参数 ::
,这句就不会执行
通过脚本启用关闭服务
启用Spooler服务
@echo off
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof
:Admin
net start Spooler
禁用Spooler服务
@echo off
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof
:Admin
net stop Spooler