【问题标题】:Restart Service on Remote System Batch在远程系统批处理上重新启动服务
【发布时间】:2014-01-18 23:07:27
【问题描述】:

只是遇到了批次问题,需要对“IF”、“ELSE”和“GOTO”进行一些说明

@echo off

sc \\RemoteServer stop "My_Service"

:query
sc \\RemoteServer query "My_Service"

if %state% == 1 goto start # state =1 indicates service is stopped
else goto query

:start
sc \\RemoteServer start "My_Service"

所以,我想要发生的事情是让服务停止,批量查询服务的状态,并根据状态进行下一步。这是必要的,因为服务需要相当长的时间才能停止。如果服务卡在3 STOP_PENDING 中,则sc start 将失败。

谁能解释我的 GOTO 失败的原因,或者可能提供更好的方法?

TNA

【问题讨论】:

  • start 是保留关键字。尝试另一个标签名称,加上语法需要这种样式if compare (do this) else (do that) 带括号。结束括号可以省略,但最好将它们包括在内以更清晰。

标签: windows batch-file cmd command command-line-interface


【解决方案1】:

使用else时,你需要在命令周围使用括号,并且else语句与右括号在同一行:

if "%state%"=="1" (goto start) else goto query

或者,如果您需要在 if 语句中执行更多操作:

if "%state%"=="1" (
    echo State is 1
    goto start
) else (
    echo State is not 1
    goto query
)

注意if条件两边的引号不是必须的,以防%state%为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2017-08-07
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多