CMD. Distinguish between administrators, user groups, etc.

ZC: Summary: " net session " (to determine whether it is an administrator), " whoami /?" (User information), " net user% username%" (user group and other information)

 

1. What does **** 1> nul 2> nul mean in batch processing?

1> nul means not to display the correct prompt for the command to run 
2> nul is not to display the error prompt 
and it is not displayed together with the correct error 
> is the redirection symbol 
nul is the meaning of 
the empty device The input of the prompt to the empty device is not displayed

 

2. In the bat code, determine whether bat is running with administrator rights, and automatically run CMD BAT with administrator rights-Urgent!-Blog Garden.html ( https://www.cnblogs.com/05-hust/p/12089839 .html )

 1. Determine whether the bat is running with administrator rights

@echo off
net.exe session 1>NUL 2>NUL && (
    goto as_admin
) || (
    goto not_admin
)

:as_admin
echo as_admin
goto end

:not_admin
echo not as admin

:end
pause

 2. Automatically run this CMD or BAT file with administrator rights

@echo off  
net.exe session 1>NUL 2>NUL && (
    goto gotAdmin
) || (
    goto UACPrompt
)
   
: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" )  
 
:begin

pause

 

3. The whoami command can find out the SID number and other information of the windows system.

 

4. How to detect whether CMD is running as an administrator _ has higher authority? -Q & A-Cloud + Community-Tencent Cloud.html ( https://cloud.tencent.com/developer/ask/112468 )

 ZC:!! AT command is still available in WIn7, and has been deprecated in WIn10 ...

AT > NUL
IF %ERRORLEVEL% EQU 0 (
    ECHO you are Administrator
) ELSE (
    ECHO you are NOT Administrator. Exiting...
    PING 127.0.0.1 > NUL 2>&1
    EXIT /B 1
)

 

5, the batch method for determining current user group _zhouzhou_ Sina blog .html ( http://blog.sina.com.cn/s/blog_3d730bdf0101bp3f.html )

@echo  off 
rem ZC: Personal feeling %% i, %% j, %% k should refer to the 1/2/3 field of the tokens split string (only guess, to be confirmed by the data to be checked) 
rem %% i is Field 1, %% j is the second field, %% k is the third field, please modify the obtained variables as needed. 
for / f "tokens = 1,2, *" %% i in (' net user% username% ^ | find / i "local group member"') do  set "users = %% j"
 echo current user% username% The user group is% users%
 pause

 ZC: On Win7x64 (company machine), the above instruction is no problem; but on the Win10x64 machine at home, an error is reported: " 'tokens' is not an internal or external command, nor is it a program or batch file that can be run. "

 ZC: I do n’t understand the for + tokens + find command above ...

 ZC: Checked some information to help understand: " net user% username% " can see the information of the user group where a user is located; try it yourself, "net user% username% ^ | find / i" local group member "" Partial information of " net user% username% " result will be printed out . Then understand, the general meaning of this bat should be: " net user% username% " to obtain the user's information-> hand the result to " find / i" local group member " " to find the information in the "local group member" section, Then the tokens are used to extract the string

 

 5.1, Bat command learning-&, &&, |, ||,>, >> symbols in batch processing-CSDN blog.html ( https://blog.csdn.net/Oliver_xpl/article/details/88717522 ) ( https : //blog.csdn.net/iloli/article/details/44339893 )

& Execute multiple commands in sequence, regardless of whether the command is executed successfully 
  Example: copy nul 5.txt & echo 666 >> 5.txt & more 5. txt 
  creates a 5.txt document and writes "666" to the 5.txt document ", Output the contents of




 5. txt. 
&& Execute multiple commands in sequence, and the subsequent commands will not be executed after encountering the wrong command 
|| Execute multiple commands in sequence, and the subsequent commands will not be executed when the correct command is encountered (ie: only the previous command is executed Only execute the following commands when there is an error) 
| The execution result of the previous command of the pipeline command is output to the next command such as: help | more > Clear the original

 content in the file before writing >> Append the content to the end of the file without clearing the original Some content mainly outputs the content originally displayed on the screen to the specified file. If the specified file does not exist, the file is automatically generated

 5.2 What does batch processing mean? Baidu knows.html ( https://zhidao.baidu.com/question/160218459.html )

^ Is an escape character in batch processing, used to escape special characters to ordinary characters. 
This FOR command executes the character in the middle of 'ipconfig ^ | find "IP Address"' as a command, and the | is not an ordinary character, but a command symbol, so you need to use the escape symbol ^ to escape Into ordinary characters, so that 'ipconfig ^ | find "IP Address" ' is full of ordinary characters, and the FOR command can be executed correctly. 
Example: 
echo > The 
result is that the command syntax is incorrect. 
echo ^> The 
result is display > this character.
echo ^^ The 
result is to display the character ^. 
In addition, these characters like ^ & | <> are special characters. When you want to use them as ordinary characters, you must add the escape ^ in front of them.

 5.3. Delims and tokens in _f for batch processing-AskTa0-Blog Park.html ( https://www.cnblogs.com/ldhbetter/p/9196599.html )

0x01 analysis of the entire sentence
 1, for / f: used to parse text, read strings, the text to be read is tmplist.txt .
2. Tokens: tokens are responsible for extracting strings. See specific examples for specific usage.
3. delims: According to the defined symbols, it is responsible for splitting the string. For example, delims == is to split the string according to the = sign.
4. in, do : that is the surface meaning, what and what to do
 5. (tmplist.txt ): text file to be operated
 6, echo %% a >> software.txt: redirect a piece of content to software.txt text

 5.4 Detailed explanation of for_f of Bat batch processing_Operation and maintenance_Crayon Xiaoxin's column-CSDN blog.html ( https://blog.csdn.net/davidhsing/article/details/1996180 )

含有/F的for
格式:
FOR /F ["options"] %%i IN (file) DO command
FOR /F ["options"] %%i IN ("string") DO command
FOR /F ["options"] %%i IN ('command') DO command

 

6、start

7 、 speech

8、

9、

 

Guess you like

Origin www.cnblogs.com/osskill/p/12676737.html