BAT script record of windows

I. Overview

This article mainly records the BAT script problems and usage items used in daily work, including the relevant introductory basic materials collected and organized in the network for later reference.

2. Grammar

3. Script

3.1. Implementation method of running BAT script with administrator privileges

1) Example 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%"\system32\net start mysql

Description: mshta.exe is an hta (HTML Application HOST: HTML application host, which can be understood as an html) file interpreter, just like CMD is an interpreter for batch files. The protocol used by the vbscript table can also be javascript; so that the mshta program can directly execute the vbs/js statement without placing the vbs/js statement in the html file; and hta is designed for local operation and can be opened with higher permissions html file.
2) Example 2: Just write it in front of the command that needs to be executed

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

3) Example 3:

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

4) Example 4: When the bat starts, first call the vbs script, and through the vbs script, call the runas part of the bat as an administrator

@ECHO OFF
setlocal EnableDelayedExpansion
color 3e
title 添加服务配置
    
PUSHD %~DP0 & cd /d "%~dp0"  ::更改当前目录为批处理脚本当前所在的目录,%0代表批处理本身,~dp是变量扩充,d(Drive的缩写)既是扩充到分区号 d: p(Path)就是扩充到脚本所在路径;
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :runas","","runas",1)(window.close)&goto :eof
:runas
    
::填写自己的脚本
    
echo 执行完毕,任意键退出
    
pause >nul
exit

Description: Relevant meaning

~0 - removes any quotation marks ("), expands %0; %0 refers to the current script, and ~ stands for various expansions;
%~f0 - expands %0 to a fully qualified pathname ("f" is file , i.e. a file)
%~d0 - expands %0 only to a drive
letter %~p0 - expands %0 only to a path
%~n0 - expands %0 only to a filename ("n" is the name filename )
%~x0 - expands %0 to a file extension only
%~s0 - path short name, i.e. the expanded path contains only short names ("s" is Short, short)
%~a0 - expands %0 to The file attribute of the file ("a" is an attribute)
%~t0 - Extend %0 to the date/time of the file ("t" time)
%~z0 - Extend %0 to the size of the file (Size size)
%~$PATH:0 looks for the directory listed in the PATH environment variable and expands %0 to the first fully qualified name found. If the environment variable name is not defined, or the file is not found, this key combination expands to Empty string
%~dp0 - only expands %0 to a drive letter and path
%~nx0 - only expands %0 to a filename and extension
%~fs0 - only expands %0 to a full with short name pathname
%~dp$PATH:0' - look for the directory listed in the path environment variable and expand %I to the first drive letter and path found.
%~ftza0 - expand %0 to DIR like an output line
%0 is the current batch file, if 0 is replaced by 1, it is the first file, 2 is the second...;%1 represents the first parameter passed to the script, and %~1 also represents the first parameter , but when the parameter contains quotation marks, remove the quotation marks;
current drive letter: %~d0
current path: %cd%
current execution command line: %0
current bat file path: %~dp0
current bat file short path: %~sdp0
set pa=%cd% Assign the current path to pa, no spaces before and after the equal sign // Windows shortcuts cannot use relative paths. You can use a batch file to get the relative path
echo %pa% show the pa variable // verify
5) Example 5:

@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) Example 6: Let BAT and CMD batches run as 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) Example 7: CMD batch automatically runs as administrator

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

Description: reproduced in the programming inn .

Guess you like

Origin blog.csdn.net/ximenjianxue/article/details/123320132