Solved

Error handling in batch

Posted on 2008-10-06
8
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

738 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