0

I have below code in my file. Please help to get desired results.

for /f %%i in (D:\servers.txt) do ( echo Server_Name:%%i>>D:\log.txt & %systemroot%\System32\sc \\%%i query service |find "STATE"  >> D:\log.txt & echo ********************************************************************************* >>D:\log.txt)

Expected output:

Server_name:XYZ Status:running
***********************************
Server_name:PQR Status:running
***********************************

What I am getting:

Server_name:XYZ 
Status:running
*******************
Server_name:PQR
Status:running
*******************
Compo
  • 36,585
  • 5
  • 27
  • 39
Pratik
  • 11
  • 2
  • Current Output: Server_name:XYZ Status:running ******************* Server_name:PQR Status:running ******************* – Pratik May 15 '20 at 11:36
  • Why not use a more appropriate methodology, WMI, instead, `%__AppDir__%wbem\WMIC.exe /Node:@servers.txt /FailFast:On /User:UsersName /Password:UsersPwd /Output:log.txt Service Where Name="MySecretServiceName" Get State /Format:CSV`. Obviously adjust the `MySecretServiceName` to the name you've omitted from your question, and modify/remove the `/User` and `/Password` global switches as needed for your specific circumstances. – Compo May 15 '20 at 13:15
  • **The output you've stated that you're 'getting' is UNTRUE!** You're specifically `echo`ing `*********************************************************************************`, and telling us that its output is `*******************`, and you're also `echo`ing `Server_Name` and telling us that it's returning as `Server_name`! – Compo May 15 '20 at 15:03
  • Worse than that however is the second line must begin with spaces followed by something like `STATE              : 1 STOPPED` or `STATE              : 4 RUNNING`, because that is what `find "STATE"` must return. That means you're not only wanting to join two lines together, you're also looking for a way to modify the output from the second command too! – Compo May 15 '20 at 15:06
  • @Compo Hi Mate, Actually I am using this output in my .net code and need to put lot of logic to get required fields value. I am ok with spaces as I am using ":" as delimiter. Can we use pipeline ?? – Pratik May 19 '20 at 11:29

0 Answers0