Solved

Error handling in batch

Posted on 2008-10-06
8
659 Views
Last Modified: 2008-10-07
I am learning how to add some error handling to my batch files.  Could someone please tell me why this does not work?
I always returns that it worked, even if I put bad entries in the banner_servers file.


For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	shutdown -f -r -m \\%%N -t 00 >> shutdownresults.txt) & (
	if errorlevel 0 echo "%%N successfully sent the reboot command" >>shutdownresults.txt) & (
	ping -n 1 -w 5000 x.x.x.x)

Open in new window

0
Comment
Question by:loftyworm
  • 4
  • 2
  • 2
8 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 22653042
IF ERRORLEVEL works differently than other IF statements.  if errorlevel 0 will be true if the errorlevel is equal to or greater than zero.  This means that it will always be true.

You probably want if %errorlevel% GTR 0 instead.

This page has more information:
http://www.ss64.com/nt/if.html
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 250 total points
ID: 22653045
Correction.  You want if %errorlevel%==0.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22653049
Don't use Errorlevel 0. Errorlevel 0 is always set. I haven't found out why yet. My guess is Errorlevel 0 is set if the previous command was run. Evaluate errorlevel 1.

In your case change if errorlevel 0 to if not errorlevel 1.

Errorlevel 1 is set if the command failed.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 11

Author Comment

by:loftyworm
ID: 22653198
:(  
I tried both ways (and added a little else statement) but it is still saying it always works


For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	shutdown -f -r -m \\%%N -t 00 >> shutdownresults.txt) & (
        if %errorlevel%==0 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%"))& (
rem 	if not %errorlevel%==1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%"))& (
	ping -n 1 -w 5000 x.x.x.x)

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22653272
if not errorlevel 1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%"))& (
      ping -n 1 -w 5000 x.x.x.x)
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 250 total points
ID: 22653356
Pretty messy the way you have it.  Also I don't think shutdown is setting the errorlevels when it cannot contact the workstation.
For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	shutdown -f -r -m \\%%N -t 00 >> shutdownresults.txt
 	if not errorlevel 1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%")
	ping -n 1 -w 5000 127.0.0.1
)

Open in new window

0
 
LVL 11

Author Comment

by:loftyworm
ID: 22653689
So its not me????
Its the shutdown.exe that is not setting the errorlevel?
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22664194
Try this.
For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	shutdown -f -r -m \\%%N -t 00 | FIND /i "The network path was not found."
 	if errorlevel 1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%")
	ping -n 1 -w 5000 127.0.0.1
)

Open in new window

0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question