Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch Scripting Help

Status
Not open for further replies.

robbyd925

IS-IT--Management
Nov 18, 2005
4
CA
Hi all,

I'm writing this batch script to start/stop some windows services using a combination of net start/stop and sc.exe. Now I need to do some verification on it, command 2 can only run when command 1 runs successfully. So let's say I issue

sc start WinService01

If this service starts successfully it should run WinService02. If it doesn't run then goto WinService05 or something like that.

I've played around with:

:begin
@echo off
sc start WinService01
if errorlevel 1 goto end
echo Success
:end
echo Failed



In this snippet when the service runs fine it will display Success, no problem. But when the service fails to run it will display Failed AND Success.

What am I missing? Or if you have a better script I'd appreciate it.

Thanks in advanced!
 
You need another goto statement to skip the "failed" message if the execution is successful:

:begin
@echo off
sc start WinService01
if errorlevel 1 goto end
echo Success
goto reallyend
:end
echo Failed
:reallyend
exit
 
Are you sure the sc command returns (sets) any errorlevel usable in a batchfile? I tried a little, but it didn't give any errorlevel either for a good or a bad result, so the entire concept may be wrong...

HTH
TonHu
 
Huh, good find TonHu, I just tested and you're right. I get an errorlevel 0 whether the service starts successfully or not. So, robby's best bet is to use "net" commands, but he still has to put in that extra "goto" statement to skip the "fail" message if the command is successful.
 
Check for the service using net start instead...

Code:
:begin
@echo off
sc start WinService01
sc query WinService01 | find /i "Running"
if errorlevel 1 goto end
echo Success
:end
echo Failed
 
Sorry - I said use net start then researched it and forgot to change the opening... sc query will do essentially the same thing when piped through find.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top