1 @echo off 2 rem Run as administrator 3 %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit 4 rem Switch to the file current directory 5 cd /d "%~dp0" 6 7 set DB1=MariaDB10.4 8 set DB2=MariaDB10.5 9 set DB3=MSSQLSERVER 10 11 set ASS=aspnet_state 12 set ZDS=ZyrhDeviceService2 13 14 :flag_loop 15 16 cls 17 18 rem echo %0 19 20 echo. 21 echo [:Loop] Please select service id: 22 call :flag_state %DB1% 1 23 call :flag_state %DB2% 2 24 call :flag_state %DB3% 3 25 call :flag_state %ASS% 4 26 call :flag_state %ZDS% 5 27 rem echo. 28 29 :flag_type 30 echo. 31 set /p "type= [:Type] Please input service id (1 - 5) (R - :Loop, Q - Exit) : " 32 33 set return=%type% 34 call :upper_case %type% return 35 set type=%return% 36 37 call :check_exit %type% 38 39 if "%type%" == "1" ( 40 call :flag_state %DB1% %type% 41 call :flag_action %DB1% %type% 42 ) else if "%type%" == "2" ( 43 call :flag_state %DB2% %type% 44 call :flag_action %DB2% %type% 45 ) else if "%type%" == "3" ( 46 call :flag_state %DB3% %type% 47 call :flag_action %DB3% %type% 48 ) else if "%type%" == "4" ( 49 call :flag_state %ASS% %type% 50 call :flag_action %ASS% %type% 51 ) else if "%type%" == "5" ( 52 call :flag_state %ZDS% %type% 53 call :flag_action %ZDS% %type% 54 ) else if "%type%" == "R" ( 55 goto flag_loop 56 ) else ( 57 goto flag_type 58 ) 59 60 goto comments 61 函数 62 根据用户输入确定不同的操作 63 参数1: 要操作的服务名称,字符串 64 :comments 65 :flag_action 66 echo. 67 rem 这里的□ (<0x08>)是一个退格符,是为了显示出后面的两个空格位置 68 set /p "action= Please input action key (S - Start, X - Stop, C - Config, R - :Type, Q - Exit) : " 69 70 set return=%action% 71 call :upper_case %action% return 72 set action=%return% 73 74 call :check_exit %action% 75 76 if "%action%" == "S" ( 77 call :flag_start %~1 %~2 78 ) else if "%action%" == "X" ( 79 call :flag_stop %~1 %~2 80 ) else if "%action%" == "C" ( 81 call :flag_config %~1 82 ) else if "%action%" == "R" ( 83 goto flag_type 84 ) else ( 85 goto flag_action 86 ) 87 goto:eof 88 89 goto comments 90 函数 91 查询服务状态,并返回明确的服务状态,如 RUNNING 或 STOPPED,接受两个参数 92 参数1: 要查询的服务名称,字符串 93 参数2: 接受返回结果的变量 94 :comments 95 :flag_state 96 set st= 97 call :flag_qc %~1 %~2 st 98 99 set fn=%~1_tmp_123.log 100 rem 查询服务状态信息,提取 STATE 内容并写入到文件中 101 sc query "%~1" |findstr "STATE" > %fn% 102 103 set value= 104 rem 读取文件内容到value变量 105 set /p value=<./%fn% 106 107 del %fn% 108 109 set index=2 110 rem 按冒号:分割字符串,并提取第2部分的内容(下标是从1开始的) 111 for /F "tokens=%index% delims=:" %%a in ("%value%") do set value=%%a 112 113 rem 按空格分割字符串,并提取第2部分的内容(下标是从1开始的) 114 for /F "tokens=%index% delims= " %%a in ("%value%") do set value=%%a 115 echo %~2 %~1 ---- %value% ---- %st% 116 goto:eof 117 118 :flag_qc 119 set fn=%~1_qc_123.log 120 rem 查询服务配置信息,提取 START_TYPE 内容并写入到文件中 121 sc qc "%~1" |findstr "START_TYPE" > %fn% 122 123 set value= 124 rem 读取文件内容到value变量 125 set /p value=<./%fn% 126 127 del %fn% 128 129 set index=2 130 rem 按冒号:分割字符串,并提取第2部分的内容(下标是从1开始的) 131 for /F "tokens=%index% delims=:" %%a in ("%value%") do set value=%%a 132 133 rem 截取字符串,从第5个字符开始(空格也算是字符) 134 set value=%value:~5% 135 136 rem 替换双空格为单个空格 137 set value=%value: = % 138 139 rem echo %~2 %~1 -------- %value% 140 141 rem 将返回值赋值给变量3 142 set %~3=%value% 143 goto:eof 144 145 :flag_start 146 echo start %~1 147 sc start %~1 148 timeout 2 149 call :flag_state %~1 %~2 150 goto flag_type 151 goto:eof 152 153 :flag_stop 154 echo stop %~1 155 sc stop %~1 156 timeout 2 157 call :flag_state %~1 %~2 158 goto flag_type 159 goto:eof 160 161 :flag_config 162 echo. 163 set /p "config= Please input config key (1 - Automatic, 2 - Manual, 3 - Disabled, 4 - Delayed Auto, R - :Type, Q - Exit) : " 164 165 call :check_exit %config% 166 167 rem 设置服务启动方式 auto - Automatic, demand - Manual, disabled - Disabled, delayed-auto - Automatic (Delayed Start) 168 if "%config%" == "1" ( 169 rem start= 这里需要一个空格 170 sc config %~1 start= auto 171 ) else if "%config%" == "2" ( 172 sc config %~1 start= demand 173 ) else if "%config%" == "3" ( 174 sc config %~1 start= disabled 175 ) else if "%config%" == "4" ( 176 sc config %~1 start= delayed-auto 177 ) else if "%config%" == "R" ( 178 goto flag_type 179 ) else ( 180 goto flag_config 181 ) 182 goto:eof 183 184 :check_exit 185 if "%~1" == "Q" ( 186 goto flag_exit 187 ) else if "%~1" == "EXIT" ( 188 goto flag_exit 189 ) else if "%~1" == "QUIT" ( 190 goto flag_exit 191 ) 192 goto:eof 193 194 :upper_case 195 set val=%~1 196 for %%i in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do call set val=%%val:%%i=%%i%% 197 set %~2=%val% 198 goto:eof 199 200 :lower_case 201 set val=%~1 202 for %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do call set val=%%val:%%i=%%i%% 203 set %~2=%val% 204 goto:eof 205 206 :flag_exit 207 exit