0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
sss
  • 5
  • 3
  • `@(For %%A In ("SA1" "SA2" "ANOther") Do @%SystemRoot%\System32\sc.exe Query %%A) > "C:\servicestatus.log"` – Compo May 20 '21 at 14:37
  • i am sorry. i will do the neeedful – sss May 20 '21 at 16:32
  • What about commenting upon my initial comment to this question too?There seems to have been little need not to have done so, when you formulated the above response. _BTW, I've noted that after more than an hour, you still have not done what you indicated above either!_ – Compo May 20 '21 at 17:41
  • i have spent good 30 mins to accept Compo answer and i don't find the checkmark KB talks about. dont know how to accept it – sss May 20 '21 at 18:10
  • To the left of the answer you wish to accept, there should be a large grayed out check mark, when you click on that, it will turn green and accept that answer! – Compo May 20 '21 at 18:28
  • Compo, i accept your answer. but i dont see the large grayed out checkmark to check and make it green. i only see options for bookmarking, activity and flagging. – sss May 21 '21 at 11:53
  • The check mark should be there [just below the up and down buttons for question voting](https://meta.stackexchange.com/a/5235). If it's not there, perhaps it has something to do with your username change. If that is the case, perhaps you should ask for clarity or assistance with that on [Meta](https://meta.stackexchange.com/questions/ask). – Compo May 21 '21 at 12:12
  • https://meta.stackexchange.com/questions/349676/checkmark-option-appears-to-be-missing-cant-accept-answer .. i got this and i am hoping this is not the case – sss May 22 '21 at 14:26
  • That answer is relating to comments, and that is not what I was referring to. You cannot accept an answer to this question as you've only received comments to it, and no answer. In your previous four questions however, three of those did receive answers, and they're the ones I was referring to in my second comment. What I will do now therefore is repeat my first comment as an answer, and then you can see if the issue is still evident. – Compo May 22 '21 at 15:04

1 Answers1

0

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"
Compo
  • 36,585
  • 5
  • 27
  • 39