help wanted with batch file output

May 9, 2011 at 05:55:51
Specs: Windows XP

I have the following code that reads from a text file of hostnames and checks if a particular service is running on it, if it does - do nothing or if its in the 'stopped' state - restart it.
My question is how I would be able to output the hostnames (that had that particular service restarted) into a textfile called 'restartedPCs.txt'.

This is giving me a huge headache and any help would be appreciated...

@echo off

:: version 1.3
:: enumerates through a text file of machines
:: check if service is running on machines in text file
:: if its already running, do nothing
:: if its not running - start it

:: assign service_name variable to a particular service
set service=uvnc_service

:: enumerate through hostnames in text file
for /F %%a in (c:\temp\find.txt) do call:logic %%a
goto end

for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr "STATE"') do (
if /I "%%H" NEQ "RUNNING" (
goto start_it
if /I "%%H" NEQ "STOPPED" (
goto already_running

echo starting %service% please wait...

:: start the service, but don't display default sc output
sc start uvnc_service > NUL

rem timer to delay confirmation of restarted service
echo Waiting 3 seconds
ping -n 1 -w 3000 > NUL
echo done

goto end

echo %service% is already running


See More: help wanted with batch file output

May 9, 2011 at 06:38:15
I don't have time to whip up a script, but here's a fun fact for whoever does: SC's errorlevel is 1056 if you attempt to start an already started service. (Also his SC QUERY statement is bugged.)

How To Ask Questions The Smart Way

Report •

May 9, 2011 at 06:50:35
I'm sure i'm either another if statement/for loop away from accomplishing the part to output the hostnames to a text file. There must be someone out who can help me with this?

Report •

May 9, 2011 at 09:29:04
Alright, I now have a minute to write something, but not the 5 required to boot up the ol' VM and try it in XP, so you get something untested. (Also, your script doesn't affect remote machines at all, which is a large part of your problem.)
for /F %%a in (c:\temp\find.txt) do sc \\%%a start uvnc_service && >> out.txt echo Started %%a

How To Ask Questions The Smart Way

Report •

Related Solutions

May 9, 2011 at 09:38:04
Hi Razor2.3;

Thanks for your help. The hostnames are in the find.txt file (each one on a new line) - I've mis-typed what i wanted to get help on. I just needed to read from the text file and then output the 'restarted' machines into another text file.

Will give this a go and report back my findings...


Report •

May 9, 2011 at 10:01:04
Hi Razor2.3

That has done the trick, removed the redundant code and only have a few lines that does everything - which is good. I'll have look at trying to clean it up so it looks a bit nicer on the screen - I don't like the default sc output when it starts a service so I will hide this and show a generic message such as service has been restarted.

Thanks for your help.

Report •

May 10, 2011 at 02:11:07
Hi Razor2.3;

I've tested your code and it sort of works, however I have 3 hostnames in my find.txt file (pc1 pc2, pc3 - one on each line). The service on pc1 is in the 'stopped' state, whilst the service on pc2 and pc3 are running. In theory, I should only see the line 'pc1 started'; however I've also noticed that the entries for 'pc2 started' and 'pc3 started' in there as well - even though there are running? If you get 5 minutes today, can you help me with this please?


Report •

May 13, 2011 at 13:07:33
You really shouldn't double-post like that. It makes me sad.

If WinXP's SC doesn't set the errorlevel like it does in Win7, then try this:

for /F %%a in (c:\temp\find.txt) do sc \\%%a start uvnc_service | find "START" && >> out.txt echo Started %%a

How To Ask Questions The Smart Way

Report •

May 17, 2011 at 07:37:50
I've managed to write the following:
only pc1 should appear in the out.txt file, but for some reason, i get nothing (doesn't even start the service).
if i change the error level to 0 (i starts the service on pc1, but echo's pc1, 2 and 3 to out.txt rather than just 1). Can you see what can be wrong with this?

set service = uvnc_service
for /F %%a in (c:\find.txt) do call :servicecheck %%a

sc \\%1 query %service% | FIND "RUNNING"

sc \\%1 start %service% && >> out.txt echo %1


Report •

May 17, 2011 at 12:04:09
I don't notice any issues with your script; I suspect your problem is in find.txt.

How To Ask Questions The Smart Way

Report •

May 18, 2011 at 00:59:52
the find.txt looks like this (I don't see anything out of the ordinary):


Report •

Ask Question