I just used Jonas' example above and created full list of 0 to 24 errorlevels. Other post is correct that net start
and net stop
only use errorlevel
0 for success and 2 for failure.
But this is what worked for me:
net stop postgresql-9.1
if %errorlevel% == 2 echo Access Denied - Could not stop service
if %errorlevel% == 0 echo Service stopped successfully
echo Errorlevel: %errorlevel%
Change stop
to start
and works in reverse.