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:
The second file just needs to be saved as a .bat file.
1: REM ***Start Here***
2: Echo Off
3: Setlocal EnableDelayedExpansion
4: FOR /F "Tokens=*" %%L IN (Servers.txt) DO (
5: SET ServerName=
6: SET ServerName=%%L
7: sc.exe \\!ServerName! query <Your Service Name> > Results.txt
8: ECHO !ServerName!
9: Find /i "RUNNING" < Result.txt
10: Find /i "STOPPED" < Result.txt
12: IF EXIST Results.txt DEL Result.txt
13: 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.