检查服务是否存在

时间:2016-03-23 13:27:58

标签: windows batch-file service

我可以在我的批次中写什么来检查服务是否存在?现在,如果我运行else if (searchBy == "Birthdate") { return View(db.Class1.Where(x => x.Birthdate.Equals(Convert.ToDateTime(search)).ToList()); } 并且该服务不存在,我的批处理只会继续下一个。我想要检查它是否存在它运行net stop否则它不会。

net stop

1 个答案:

答案 0 :(得分:1)

您可以使用sc.exe查询服务状态,然后检查ERRORLEVEL

for %%S in ("service1" "service2" "service3") do (
    sc query %%S > nul
    if ERRORLEVEL 0 (sc stop "%%~S")
)
相关问题