Windows批处理文件eq此时出乎意料

时间:2017-11-13 07:46:54

标签: windows batch-file

我正在编写一个将安装服务的Windows批处理脚本。首先,我需要查找服务是否已存在。如果服务存在,则必须检查状态。如果状态正在运行,则必须停止并删除该服务。

这是我的代码:test.bat。我从命令行运行它。

for /F "tokens=3 delims=: " %%H in ('sc query "IBMLibertyProfile" ^| findstr "STATE" ') do (
  if /I "%%H" EQ "RUNNING" (
   sc stop "IBMLibertyProfile"
  )
)

我收到错误:

  

C:> test1.bat EQ此时出乎意料。

     

C:> if / I“%H”EQ“RUNNING”(

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

试试这个:

@rem If the service doesn't exist, exit.
@sc query IBMLibertyProfile > NUL 2>&1
@if %ERRORLEVEL% neq 0 @exit /b 0

@rem If the service is already stopped, delete it.
@sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
@if %ERRORLEVEL% neq 0 @goto :DeleteService

@rem No matter it's state, tell it to stop.
@sc stop IBMLibertyProfile

@rem Wait for it to stop.
@set _WaitLoopCount=0
:StoppedWait
@if _WaitLoopCount equ 10 @goto :ServiceWaitTimeout
@timeout /t 3 > NUL 2>&1
@sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
@if %ERRORLEVEL% neq 0 @goto :StoppedWait

@rem Delete the service and exit.
:DeleteService
@sc delete IBMLibertyProfile
@exit /b 0

:ServiceWaitTimeout
@echo Service failed to reach the STOPPED state. Reboot the computer and try again.

注意:如果您的服务表现不佳,脚本可能会挂起。我将留给您研究如何处理sc删除失败。