服务要批量停止

时间:2016-03-21 12:21:19

标签: windows batch-file windows-services scheduled-tasks

我想检查服务是否正在运行。如果它是我希望它被停止。我确实有很多服务,所以我希望它可以在数组中运行或者某些服务。

据我所知:

for /F "tokens=3 delims=: " %%H in ('sc query "MyServiceName" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
  REM Put your code you want to execute here
  REM For example, the following line
  net stop"MyServiceName"
)

然后我有例如service1,service2,service3和service4

如果不需要将此for循环写入四次,这怎么可以加入呢?

1 个答案:

答案 0 :(得分:0)

for %%S in ("service1" "service2" "service3") do (
  for /F "tokens=3 delims=: " %%H in ('sc query "%%~S" ^| findstr "        STATE"') do (
   if /I "%%H" NEQ "RUNNING" (
   REM Put your code you want to execute here
   REM For example, the following line
   net stop"%%~S"
  )

)