?
Solved

Problem with batch script using SC query command

Posted on 2012-09-20
11
Medium Priority
?
1,053 Views
Last Modified: 2012-09-24
I am trying to see if multiple service are stopped or started...its not working, any batch script experts that can help me?  The services in the services.txt file appears to get cut off, they are over 35 characters long.  From the output file i just need the server name and services status?  

for /f %%a in (hosts.txt) do call :serviceconfig %%a
goto :eom
:serviceconfig for /f %%b in (services.txt) do (sc \\%1 query %%b) >> output.txt
:eom

Open in new window

0
Comment
Question by:mystikal1000
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 12

Expert Comment

by:mlongoh
ID: 38418801
Try this:

for /f %%a in (hosts.txt) do call :serviceconfig %%a
goto :eom
:serviceconfig
for /f %%b in (services.txt) do (sc \\%1 query %%b) >> output.txt
:eom
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 38418872
FOR /F will only store the first chunk of text, until a whitespace delimiter is found. You should use either "delims=" or "tokens=*" to read the whole line into the loop var:
for /f "delims=" %%a in (hosts.txt) do call :serviceconfig %%a
exit /b

:serviceconfig
for /f "delims=" %%b in (services.txt) do sc \\%1 query %%b >> output.txt
exit /b

Open in new window

Further, there is not much of a reason to split that simple commands into a subproc:
for /f "delims=" %%a in (hosts.txt) do (for /f "delims=" %%b in (services.txt) do ^
sc \\%%a query "%%b"
) >> output.txt

Open in new window

This has another improvement, as the data for each host is written in one go into the log file, speeding that part up.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38418874
I suspect your problem might be service names with embeded spaces?  If so, give this a try and see if it helps, plus it's a bit simpler...

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

Open in new window

~bp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:mystikal1000
ID: 38419253
Qlerno -

This one worked below, the second one was giving me sc /? help commands.
However I want the server name, service name and state=running or stopped as its output?

I tried the other scripts and they are not working correctly.  Appreciate the help!!!

for /f "delims=" %%a in (hosts.txt) do call :serviceconfig %%a
exit /b

:serviceconfig
for /f "delims=" %%b in (services.txt) do sc \\%1 query %%b >> output.txt
exit /b

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38419342
==> I tried the other scripts and they are not working correctly

I just retested the script I posted in http:#a38418874 and it worked fine.

Granted it lists the info on the specified services, but your new output format wasn't shared earlier.  What did you get on the screen, and in the output.txt file when you ran my script?

~bp
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 38420135
Bill, your script has a closing parens too much in line 4.
@echo off
for /f %%a in (hosts.txt) do (
  for /f "tokens=*" %%b in (services.txt) do (
    sc \\%%a query "%%~b" >> output.txt
  )
)

Open in new window

mystikal1000,

It does not matter whether you use "tokens=*" or "delims=" here, so please try Bill's (corrected) suggestion.

Your requested output format has never been subject to this question - and it is a completely different beast to format SC output. VB Script or PowerShell are much better suited for bulk checking service status. For example, PowerShell allows this simple command:
Get-Service -ComputerName (get-content hosts.txt) -Name (get-content services.txt) |
  select MachineName, ServiceName, Status |
  out-file -encoding ASCII output.txt

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38420360
Interesting Clemo, since it actually worked here with that in.  But you're right it doesn't belong there, thanks for the extra eyes.

~bp
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 38428747
Bill's solution worked, however can I get the server name, Service name's and stopped or started only in the output file.  That will help a great deal!!!!

I appreciate it.
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 38429649
Something like this?
@echo off
setlocal EnableDelayedExpansion
set svc=
set state=
echo ------ %date% %time% ------ >> output.txt
for /f %%a in (hosts.txt) do (
  for /f "tokens=1-3 delims=: " %%A in ('sc \\%%a query ^| findstr /L "SERVICE_NAME STATE"') do (
    if %%A* == SERVICE_NAME* echo %%B | findstr /BIL /G:services.txt >nul && set svc=%%B|| set svc=
    if defined svc if %%A* == STATE* echo %%a - !svc! - %%C >> output.txt
  ) 
)

Open in new window

It should even be faster than the original code, as we are running SC only once per host. On the other hand FINDSTR needs to scan the services.txt file twice per service detected on the remote machine.
You can change the output format to your liking, e.g. using semicolons or pipes as separator, making it easier to import into other tools.

BTW, you will not get any output for services not found.
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1000 total points
ID: 38429770
Here's how I would modify my script to accomplish that:

@echo off
setlocal EnableDelayedExpansion
for /f %%a in (hosts.txt) do (
  set Server=%%a
  for /f "tokens=*" %%b in (services.txt) do (
    set Service=%%~b
    for /f "tokens=1-2* delims=: " %%c in ('sc \\!Server! query "!Service!"') do (
      if "%%c" EQU "STATE" echo !Server!,!Service!,%%e >> output.txt
    )
  )
)

Open in new window

~bp
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 38429806
Oh, I forgot a letter in my FINDSTR, it should sound /BILL :D (just kidding).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

809 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