Setting up notification of an NT service state

I use a command;
net start "<service-name>" to start and
net stop "service-name>" to stop the service.
I do this in a batch file, scheduled to run unattended.
Are there command(s) I can include in the batch file to check if the service starts/stop successfully? Such that if a return code is detected, I could then use a utility, say, Postie to email out notifications to individuals.
Given the objective, would this be so old world, and that there are more contemporary ways of doing so?
Thanks, Experts.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
there are other ways, but if you are happy with that why not.  i cant check what thwe errorlevels are at the moment but would assume net start / stop do have errorlevels, if not we could parse their output...


@echo off
set log=c:\dir\logfile.txt
Net stop >%log% 2>&1
set stoperror=%errorlevel%
if %stoperror% GEQ 1 call error STOP %stoperror%

net start >>%log% 2>>&1
set starterror=%errorlevel%
if %starterror% GEQ 1 call error START %starterror%

net start |find /i "Service Name" || call error "NOT STARTED" 0

exit /b

echo %date% %time% There was an error %~2 during %~1 >> %log%
echo Send message here... Postie, blar, vbscript etc. %log% contains error info.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This is just informative.  dragon_it is very conversant with the "best methods" for what you want to achieve, and may return with more info.

From my previous notes these are the NET command's exit codes, but I cannot remember where I got them from originally:

0 = Success
1 = Not Supported
2 = Access Denied
3 = Dependent Services Running
4 = Invalid Service Control
5 = Service Cannot Accept Control
6 = Service Not Active
7 = Service Request Timeout
8 = Unknown Failure
9 = Path Not Found
10 = Service Already Running
11 = Service Database Locked
12 = Service Dependency Deleted
13 = Service Dependency Failure
14 = Service Disabled
15 = Service Logon Failure
16 = Service Marked For Deletion
17 = Service No Thread
18 = Status Circular Dependency
19 = Status Duplicate Name
20 = Status Invalid Name
21 = Status Invalid Parameter
22 = Status Invalid Service Account
23 = Status Service Exists
24 = Service Already Paused

The descriptions can be pretty ambiguous though.  For example you will usually get an exit code of 2 if you type an invalid service name!

There is also the informative line you will see when something goes wrong, like:
"The requested service has already been started. More help is available by typing NET HELPMSG 2182"
The HELPMSG number does not relate in any way to the error codes from what I have determined.  They go up to 4 digits, but not all are used (attached Excel File with ones used in WinXP if you really, really want to know - not all output lines checked though ;-).

You can, however, use the NET HELPMSG numbers something like this as an alternative test (on the FIND command rather than the NET command) without seeing the program output on screen:

NET START SomeService 2>&1 | FIND "2182"
IF ERRORLEVEL 0 ECHO SomeService already started & GOTO :ServiceStarted

The NET HELPMSG number may also appear in an error message, for example if you try to run a NET STOP command on an unknown (not installed) Service:
"System error 1060 has occurred.
The specified service does not exist as an installed service."

Remember that when you have multiple Exit Codes where more than one could apply and you want to specifically test for more than one, start with the highest one when testing:

if errorlevel 24 echo Some Message & goto :Code24
if errorlevel 23 echo Some Other Message & goto :Code23
etc, etc
if errorlevel 0 echo Success & goto :EOF

The default    IF ERRORLEVEL 1    means 1 and above.
You can instead use:
if %errorlevel"=="1"
if %errorlevel% equ 1
to test for the exact exit code.

Other Compare-Ops are:
neq - Not Equal To
gtr - Greater Than
lss - Less Than
geq - Greater Than Or Equal To
leq - Less Than Or Equal To
The /i (treat as case insensitive) switch with the IF command is not applicable to numbers).

As I say, just informative.
Steve KnightIT ConsultancyCommented:
thanks bill.  out and about still today and tomorrow too.... Paid work so cant knock it ... So not much time to be here.

i hadnt seen that list of error codes before for net.exe so is useful here too.

Should be able to use sc.exe too if available i suppose.

Garychu -has that given you enough between us (pls split points of course) or need some more clarification?

garychuAuthor Commented:
Thanks, Experts.
I am grateful for the trouble taken and the benefits of knowledge and experience shared.
I think I now have enough to try out a few things before coming to a final solution.
Steve KnightIT ConsultancyCommented:
No problem.... Just ask another q if you need more and we'll see it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.