The realization method of let bat batch process run with administrator authority

1. The first method

Some computers are logged in by non-administrators. When running a program, you need to prompt whether to run it. The solution is as follows:

@ 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%"\system32\net start mysql

2, bat script to obtain administrator permissions

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

It can be achieved by writing at the top of the bat file.

3. Execute commands with administrator rights in the bat script

Add the following command to the first line of the bat script file:

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

4. Run the batch (bat) file automatically as an administrator

In daily operation and maintenance work, in order to facilitate system installation or configuration for windows users, using the batch processing (bat file) that comes with Windows is the simplest and fastest method.

However, the batch script will not be run as an administrator by default. Under normal circumstances, I will name the script "XXXXXX (please right-click and run as an administrator!).bat", but some users will always ignore this Prompt, run directly. At this time, due to insufficient permissions, the script fails to run. We can use a workaround, when the bat starts, first call the vbs script, through the vbs script, call the runas part of the bat as an administrator

Our script can be written under runas, so just click on the bat script to run as an administrator

The sample code is as follows:

@ECHO 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. Execute the bat script with administrator rights

Just add the following script to the beginning of the .bat file you wrote, and then double-click the .bat file to execute with administrator privileges

@echo 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 已获取管理员权限

6. How to run BAT and CMD batch processing as an administrator

How to make BAT and CMD batches run as administrator?

Some batch processing requires very high authority execution to achieve the effect we need,

Put the following code at the top of the batch to run as an administrator.

@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

7. The correct method for command line CMD batch processing to automatically run as an administrator

Fixed the problem when there are spaces in the path

Add at the beginning of the batch:

Suitable for no parameters

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c "^&chr(34)^&"%~0"^&chr(34)^&" ::","%cd%","runas",1)(window.close)&&exit

Apply to one parameter

%2 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c "^&chr(34)^&"%~0"^&chr(34)^&" "^&chr(34)^&"%~1"^&chr(34)^&" ::","%cd%","runas",1)(window.close)&&exit

More parameters can be deduced by analogy. When
running batch processing, add one more parameter::, this sentence will not be executed.
In order to be compatible with 8.3 short path, you can replace% 0 with% s0, etc.
Theoretically, there is no problem, but the startup path sometimes does not Reliable, you may need to pushd or cd /d later

8. Several ways to run bat files with administrator rights

1. Create a bat shortcut, and then right-click the shortcut -> properties -> advanced -> Run as administrator.
2. Download the bat-to-exe tool, convert bat to exe, and then right-click exe–>properties–>Compatibility–>Run as administrator.
3. Run cmd with administrator rights, and then run the corresponding bat in the dos box.

Guess you like

Origin blog.csdn.net/CSDN_C2/article/details/108408158