我有以下脚本来检查服务的状态,这很好。但是我无法弄清楚如何调整它来做很多服务。如果任何服务未运行,并且指定了哪些服务未运行,则结果将发送电子邮件。
我可以为每个要检查的服务重复此操作,但如果有可能将其压缩到一个代码块中会更好。任何想法?
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
)
感谢帮助
答案 0 :(得分:2)
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。
请记住批次中的%A变为%% 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