-1

Perhaps I am not searching with the correct terms, but I can't seem to find the answer anywhere. I am trying to get a specific message to be displayed when a service errors out with "...due to a logon failure", but I don't know what I need to have entered into the batch file. With the help of this article, this is what I have so far:

@echo off
echo This will start your MYSERVICES. If you do not wish to do this, please close this window, otherwise: 
pause
for /F "tokens=3 delims=: " %%H in ('sc query "MYSERVICE1" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
   NET START "MYSERVICE1"
  )
)
for /F "tokens=3 delims=: " %%H in ('sc query "MYSERVICE2" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
   NET START "MYSERVICE2"
  )
)
@echo off
echo Your services should now be started! 

@echo off

echo If any errors came up or you still cannot get into your software, be sure to give Tech Support a call at 1-800-xxx-xxxx. 

@echo off
echo To close this window:
pause

It works fine to start the services when they are not running, but I want the line for

@echo off

echo Your services should now be started!

to return a different message if the previous command ends up erring out with the 1069 error for "...due to a logon failure". Is there any way to do this? Possibly by querying the error? I'm new to this so I'm not sure what I need to be looking at.

Thanks!

Community
  • 1
  • 1
Trevor
  • 141
  • 11
  • Take a look on [Sc failure](http://technet.microsoft.com/en-us/library/cc742019.aspx) and read also all documentation pages for `sc` written by Micrsoft listed on left side of the referenced webpage. – Mofi Aug 15 '14 at 17:49
  • I cannot put in the actual service name for confidentiality reasons, but I have edited the post to include where I got the info from and to follow more closely to what I actually have in my batch. As for the article you linked me, I will be sure to read that and be back if I cannot get it to work. Thanks! – Trevor Aug 15 '14 at 18:34

1 Answers1

0
SC Start myservice | findstr /i /c:"1069" && Echo Service Failed

Findstr returns a non zero errorlevel if text not found. && only executed if prev command returned 0.

From MSDos 6.22 Help File.

│The following list shows each exit code and a brief description of its
│meaning:
│
│0
│    The search was completed successfully and at least one match was found.
│
│1
│    The search was completed successfully, but no matches were found.
│
│2
│    The search was not completed successfully. In this case, an error
│    occurred during the search, and FIND cannot report whether any matches
│    were found.
│
│You can use the ERRORLEVEL parameter on the  command line in a batch
│program to process exit codes returned by FIND.
&    seperates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

&gt    output to a file

&gt&gt    append output to a file

&lt    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatinate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command

%&ltnumber&gt (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

%* (%*) the entire command line.

%&lta letter&gt or %%&lta letter&gt (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.


Noodles
  • 1,981
  • 1
  • 11
  • 4