windowsbatch-fileservicesc.exe

querying the state of multiple Windows services without an external file


I have to use sc.exe to log the state of a few services to a text file.

I cannot use an external text, or ini file to list my windows services.

So whilst the following works:

for /f "delims=" %%A in (list.txt) do sc query %%A >> C:\servicestatus.log

I cannot use it, (because it is using an external text file).

So I have to pass all the service names, something like:

FOR /F "tokens=2,3 delims=: " %%A in (SC QUERY "SA1" ^& SC QUERY "SA2" ^& ) do sc query %%A >> C:\servicestatus.log

…but that doesn't output what I require.

Could someone please help me to make the adjustment correctly.


Solution

  • Here's my initial comment as an answer.

    As a single line :

    @(For %%A In ("SA1" "SA2" "ANOther") Do @%SystemRoot%\System32\sc.exe Query %%A) > "C:\servicestatus.log"
    

    As a multiline :

    @Echo Off
    (
        For %%A In (
            "SA1"
            "SA2"
            "ANOther"
        ) Do (
            %SystemRoot%\System32\sc.exe Query %%A
        )
    ) > "C:\servicestatus.log"