At my current project there are times where I have to restart a bunch of servers at a time. On these servers there is a particular service which I want to make sure it has started and is running. There’s the sc.exe command which allows your stop, start, query service statuses from the command prompt.
Here’s a small batch script I wrote which runs through a list of server names and prints out the status. You’ll need two files.
First file includes the server names and is called Servers.txt:
Server1
Server2
Server3
Server4
The second file just needs to be saved as a .bat file.
REM ***Start Here***
Echo Off
Setlocal EnableDelayedExpansion
FOR /F "Tokens=*" %%L IN (Servers.txt) DO (
SET ServerName=
SET ServerName=%%L
sc.exe \\!ServerName! query <Your Service Name> > Results.txt
ECHO !ServerName!
Find /i "RUNNING" < Result.txt
Find /i "STOPPED" < Result.txt
)
IF EXIST Results.txt DEL Result.txt
REM ***END HERE***
This script will print out the server name and if your particular service is RUNNING or STOPPED. You can expand this as you would like.
~david