使用选项查询服务状态的Bat文件

时间:2014-06-18 19:25:06

标签: batch-file

我有这个查询来检查服务的状态,并根据该状态给出选项,但我在错误部分有一些问题。 IT似乎只是工作,没有选择的选项。即使这样,它也只是第一次运作。你可以看到我有3个选项但是如果我选择选项4它默认为选项1.请看看并解释我缺少的内容。

    @echo off

ECHO Status of MGT System Manager Service

Goto Welcome

ECHO.



:Welcome

sc query MGTSM | findstr /i "STATE"

ECHO What would you like to do?


ECHO 1. Start Service

ECHO 2. Stop Service

ECHO 3. Exit

set /p choice=Select your choice.

if '%choice%'=='' ECHO "%choice%" is not valid please try again. 

if '%choice%'=='1' goto Start Service

if '%choice%'=='2' goto Stop Service

if '%choice%'=='3' goto Exit

ECHO.



:Start Service

net start MGTSM

goto Welcome





:Stop Service

net stop MGTSM

goto Welcome



:Exit

exit

1 个答案:

答案 0 :(得分:0)

如果您不输入任何内容,则

set /p不会触及该变量(只需输入ENTER)。因此,您应该删除变量:

:Welcome
....
set "choice="      //this deletes %choice%
set /p "choice=Select your choice: "
if '%choice%'=='1' goto StartService
if '%choice%'=='2' goto StopService
if '%choice%'=='3' goto Exit
REM this line is only reached, when all of the "IF ... GOTO"  above failed
echo not valid input: "%choice%"
goto welcome       
...

标签和goto有空格问题:goto start service将转到“开始”,忽略“服务”。与标签相同::Start Service,标签为:Start,“服务”将被忽略。