我正在使用以下BAT文件来重新启动远程服务:
sc \\MyServer stop MyRemoteService sc \\MyServer start MyRemoteService
这对于一台计算机来说非常适用,但我想用它来重新启动服务器场中的服务。 我可以列出每个服务器在我的batch file,但我只想重新启动服务,如果他们已经在运行。 例如,如果该服务正在服务器场中的一台服务器上运行,请重新启动该服务,但是如果该服务尚未运行,则将其停止。
有什么办法可以远程完成这个任务吗? 我没有和SC结婚,还有另外一个这样的计划。
这比Izzy的答案稍微详细一些,但是在“STOP_PENDING”状态允许更长的服务持续时间。
假设在命令行中指定的文本文件中的服务器的格式为:
SERVERNAME1 SERVERNAME2 ...
和脚本:
@echo off set SVC=ServiceName if "%1"=="" goto end for /f %%i in (%1) do call :do_bounce %%i goto end :do_bounce rem Query for service running and bail if not sc \\%1 query %SVC% | find "RUNNING" if errorlevel 1 goto end rem Stop the service and loop checking for it to stop sc \\%1 stop %SVC% :check_stopped ping 127.0.0.1 -n 2 >NUL 2>NUL sc \\%1 query %SVC% | find "STOPPED" if errorlevel 1 goto check_stopped rem Restart the service sc \\%1 start %SVC% :end
对于这个让我高兴的问题,这是一个肮脏的答案:)
sc \\MyServer query MyRemoteService | find "RUNNING" && echo Service running - now stopping && sc \\MyServer stop MyRemoteService && ping 127.0.0.1 -n 10 && echo Now starting service && sc \\MyServer start MyRemoteService || echo Service not running
Powershell 2.0有一个重新启动服务commandlet
你可能想在StackOverflow上检查这个问题 。
它提到了为什么你可能要考虑从每个单独的机器(使用Windows服务监控工具)进行监控,而不是依靠networking。