?
Solved

Batch file to list specific running services

Posted on 2009-05-05
4
Medium Priority
?
1,563 Views
Last Modified: 2012-05-06
Hi, I need a batch file to be able to extract a list of machine names from the 'online.txt' file, ie,

for /f %%A in ('type "online.txt"') do (sc \\%%A query "servicename" | find "RUNNING")

then query a specific list of services running on remote machines, and output the results on screen. eg,
<MACHINENAME> <SERVICE> <RUNNING>
0
Comment
Question by:paurat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24308392
Give this a try.

default sc query is running services

stopped services
sc query state= inactive

all services
sc query state= all
@for /f "tokens=*" %%A in ('@type "online.txt"') do @for /f "tokens=1,* delims=: " %%B in ('@sc \\%%A query ^| @find /i "display_name:"') do @echo %%A,%%C,RUNNING

Open in new window

0
 

Author Comment

by:paurat
ID: 24313555
I am liking this....., but I could really do with looking for four specific services. eg, Print Spooler, McShield, + another 2 (that have not been decided on yet!). The output from listing all the services on the machines on the list would be a little too long to sort through on a daily basis.

Other than this minor point, the output from your script is perfect.

SERVICE_NAME: Spooler
DISPLAY_NAME: Print Spooler
        TYPE               : 110  WIN32_OWN_PROCESS (interactive)
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 1000 total points
ID: 24315599
Let only those names pass through.

I'd use findstr for this.

findstr /i /c:"Print Spooler" /c:"mcshield" /c:"..." /c:"..."

If you don't want to use 2 find statements.

@for /f "tokens=*" %%A in ('@type "online.txt"') do @for /f "tokens=1,* delims=: " %%B in ('@sc \\%%A query ^| @findstr /i /c:"DISPLAY_NAME: Print Spooler" /c:"DISPLAY_NAME: mcshield" /c:"DISPLAY_NAME: ..." /c:"DISPLAY_NAME:..."') do @echo %%A,%%C,RUNNING

This was actually built to export to a file quite nicely.

(@for /f "tokens=*" %%A in ('@type "online.txt"') do @for /f "tokens=1,* delims=: " %%B in ('@sc \\%%A query ^| @findstr /i /c:"Print Spooler" /c:"mcshield" /c:"..." /c:"..." ^| @find /i "display_name:"') do @echo %%A,%%C,RUNNING)>RunningServicesReport.csv


@for /f "tokens=*" %%A in ('@type "online.txt"') do @for /f "tokens=1,* delims=: " %%B in ('@sc \\%%A query ^| @findstr /i /c:"Print Spooler" /c:"mcshield" /c:"..." /c:"..." ^| @find /i "display_name:"') do @echo %%A,%%C,RUNNING

Open in new window

0
 

Author Closing Comment

by:paurat
ID: 31577944
All yours. Many thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question