批量检查多个服务的状态 [英] Check State of Multiple Services in Batch

查看:118
本文介绍了批量检查多个服务的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本来检查服务的状态,这工作正常。但我不知道如何调整这个做许多服务。

I have the following script to check the state of a service, which works fine. But I can't figure out how to adjust this to do many services. The outcome would send an email if any of the services were not running with the email specifying what services were not running.

我可以为每个服务重复此操作,但检查,但是,如果任何服务没有运行的电子邮件指定什么服务没有运行,结果会发送电子邮件。如果有办法把它压缩成一个代码块,如果可能的话会更好。任何想法?

I could just repeat this for each service to check but would be better if there was a way to condense it into one block of code if possible. Any Ideas?

Set Service=Service1

for /F "tokens=3 delims=: " %%H in ('sc query %Service%      ^| findstr "        STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo %Service% is not Running >> ServiceCheck.txt
call "Email Script with the ServiceCheck log attached"
net start %Service%
)
REM Do nothing
)

欣赏帮助

推荐答案

for /f "delims=" %A in (servicelist.txt) do sc query %A |findstr /c:"STOPPED"&&echo %A>>servicestart.log&sc start %A>>servicestart.log

在servicelist.txt中,并将任何开头的内容写入servicestart.log。

This goes through a list of services in servicelist.txt and writes anything it starts to servicestart.log.

记住批次%A成为%% A。

Remember in a batch %A becomes %%A.

& seperates commands on a line.

&& executes this command only if previous command's errorlevel is 0.

|| (not used above) executes this command only if previous command's errorlevel is NOT 0

> output to a file

>> append output to a file

< input from a file

| output of one command into the input of another command

^ escapes any of the above, including itself, if needed to be passed to a program

这篇关于批量检查多个服务的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆