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

标签 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 was unexpected at this time.

C:> if /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 删除失败的问题。

关于windows批处理文件eq此时出乎意料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47259350/

相关文章:

c++ - 为什么写入此套接字会丢弃读取缓冲区(这是真的发生了什么)?

.net - 无法在 Windows 7 中安装第三方 Windows 服务

在 Ubuntu 上运行 Java 时间比在 Windows 上运行缩短 1 小时

用于导出的 MySQL 批处理文件调用刷新表

windows - 如何在 Windows 命令行中用不同的颜色回显

windows - 使用批处理文件附加到系统 PATH 变量值

c++ - Visual C++ 库 DLL 是否已本地化?

python 2 [错误 32] 进程无法访问该文件,因为它正被另一个进程使用

batch-file - 使用批处理文件 SHIFT 命令

command-line - 查找注册表项值的问题