Windows批处理文件均衡器此时出乎意料



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

这是我的代码:测试.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 情商此时出乎意料。

C:> 如果/I "%H" EQ "RUNNING" (

如何解决此错误?

试试这个:

@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删除失败。

最新更新