SC Query Script to only show service name and state

CMILLER
CMILLER used Ask the Experts™
on
CN.txt - List of computernames
services.txt - list of services to check

@echo off
 for /f %%a in (CN.txt) do (
   echo %%a >> Output.txt
   for /f "tokens=*" %%b in (services.txt) do (
     sc \\%%a query "%%~b" >> Output.txt
   )
 )
 pause

How do I clean this script up to only show the Service_Name and state?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
It is a bad idea to try to parse sc output that way, because it needs too much effort. Just use PowerShell instead, e.g.
powershell "get-service | format-table -auto name, status"

Open in new window

or
powershell "get-service | Select name, status | Out-File Output.txt"

Open in new window

Author

Commented:
How do I use that for one service? the service I am looking for in the "Windows Agent Service"

Author

Commented:
and how do I add a list of computernames ?
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
powershell "get-service 'Windows Agent Service' -ComputerName pc1, pc2, pc3 | Select name, status | Out-File Output.txt"

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial