• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

让bat以管理员权限运行

互联网 diligentman 2周前 (01-14) 11次浏览

1、第一种方法

@ echo off
%1 %2
ver|find "5.">nul&&goto :Admin
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof
:Admin
//在你的bat开头加上上面的命令即可
//下面是你需要执行的命令
set path=%~dp0
echo install mysql service...
echo %path%
cd %path%bin
mysqld.exe --remove mysql
mysqld.exe --install mysql
echo start mysql Serviceo
"%SystemRoot%"system32net start mysql

2、bat脚本获取管理员权限

[@echo](https://my.oschina.net/echolee1987) off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"

3、bat脚本中以管理员权限执行命令

%1 mshta vbscript:CreateObject(“Shell.Application”).ShellExecute(“cmd.exe”,"/c %~s0 ::","",“runas”,1)(window.close)&&exit

4、自动以管理员身份运行批处理(bat)文件

[@ECHO](https://my.oschina.net/echolee1987) OFF
setlocal EnableDelayedExpansion
color 3e
title 添加服务配置
PUSHD %~DP0 & cd /d "%~dp0"
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :runas","","runas",1)(window.close)&goto :eof
:runas
::填写自己的脚本
echo 执行完毕,任意键退出
pause >nul
exit

5、以管理员权限执行bat脚本

[@echo](https://my.oschina.net/echolee1987) off&color 17
if exist "%SystemRoot%SysWOW64" path %path%;%windir%SysNative;%SystemRoot%SysWOW64;%~dp0
bcdedit >nul
if '%errorlevel%' NEQ '0' (goto UACPrompt) else (goto UACAdmin)
:UACPrompt
%1 start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&exit
exit /B
:UACAdmin
cd /d "%~dp0"
echo 当前运行路径是:%CD%
echo 已获取管理员权限
{{o.name}}


{{m.name}}


喜欢 (0)