Batch file error reporting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've created a batch file that will schedule a task on a list of computers.
I'v tried to add an error log that will report if there where any errors. It
alwasys reports the same error even if it fails. I'm not sure if I have the
error handling setup correct please help.

Here is the code
@Echo off
cd\

cls

Set /P Ti=Enter the time you wish job to run:
Set /p LO=Enter the location of script or program to run:

::1. Configure the path to the PS input list
Set PCList=c:\it\pc.txt

For /F "tokens=*" %%i in (%PCList%) do (Set Node=%%i) & (Call :Remote)

GOTO :EOF

:remote

at \\%Node% %ti% /interactive "%LO%"


if not errorlevel 1 goto end
echo An error occurred during
:end
echo Job completed: %node% >> c:\logs\batch.log
 
mattr2110 said:
I've created a batch file that will schedule a task on a list of computers.
I'v tried to add an error log that will report if there where any errors. It
alwasys reports the same error even if it fails. I'm not sure if I have the
error handling setup correct please help.

Here is the code
@Echo off
cd\

cls

Set /P Ti=Enter the time you wish job to run:
Set /p LO=Enter the location of script or program to run:

::1. Configure the path to the PS input list
Set PCList=c:\it\pc.txt

For /F "tokens=*" %%i in (%PCList%) do (Set Node=%%i) & (Call :Remote)

GOTO :EOF

:remote

at \\%Node% %ti% /interactive "%LO%"


if not errorlevel 1 goto end
echo An error occurred during
:end
echo Job completed: %node% >> c:\logs\batch.log

You have to enable delayed expansion so that %Node% gets
set the way it should. You must also ensure that your log directory
exists. Try this variant:

Line1 @Echo off
Line2 setlocal EnableDelayedExpansion
Line3
Line4 ::1. Configure the path to the PS input list
Line5 Set PCList=c:\it\pc.txt
Line6 if not exist c:\logs md c:\logs
Line7
Line8 Set /P Ti=Enter the time you wish job to run:
Line9 Set /p LO=Enter the location of script or program to run:
Line10 if "%Ti%"=="" goto :eof
Line11 if "%LO%"=="" goto :eof
Line12
Line13 For /F "tokens=*" %%i in (%PCList%) do (
Line14 at \\%%i %ti% /interactive "%LO%"
Line15 if !ErrorLevel!==0 (
Line16 echo Job scheduled on %%i >> c:\logs\batch.log
Line17 ) else (
Line18 echo An error occurred when scheduling the task on %%i >>
c:\logs\batch.log
Line19 )
Line20 )
Line21
Line22 endlocal
 
- Test the batch file from a Command Prompt before
scheduling it.
- Check the Task Scheduler log file.
 
Task Scheduler log did not help. Like I said the batch file is working (the
job gets scheduled) but nothing is is being logged to the log file that I'm
trying to report errors to (>> c:\it\pcinfo\batch.log).
 
This can happen for a number of reasons:
- The folder c:\it\pcinfo does not exist
- c:\it\pcinfo\batch.log is a folder
- The task does not have sufficient access rights to the log file

Run the job under the ***command prompt*** and under
the ***same account*** as you scheduled it. This will
immediately show you the cause of the problem!
 
The folder c:\it\pcinfo does exist
c:\it\pcinfo\batch.log is not a folder

I ran the batch file under the command prompt and it keeps repeating
Enter the time you wish job to run:
Enter the location of script or program to run:

Why does it run differently from the command prompt? Still clueless as to
why nothing is in the log file.

Here is how the script looks

@Echo off
setlocal EnableDelayedExpansion

::1. Configure the path to the PS input list
Set PCList=c:\it\pc.txt

Set /P Ti=Enter the time you wish job to run:
Set /p LO=Enter the location of script or program to run:
if "%Ti%"=="" goto :eof
if "%LO%"=="" goto :eof

For /F "tokens=*" %%i in (%PCList%) do at \\%%i %ti% /interactive "%LO%"

if !ErrorLevel!==0 (
echo Job scheduled on %%i >> c:\it\pcinfo\batch.log
) else (
echo An error occurred when scheduling the task on %%i >>
c:\it\pcinfo\batch.log
)
)

endlocal
 
Ok now it runs from the command prompt. The first PC in the txt file
scheduled put the second one returns network path not found.

Still nothing logged in the log file??
 
mattr2110 said:
Ok now it runs from the command prompt. The first PC in the txt file
scheduled put the second one returns network path not found.

Still nothing logged in the log file??

You need to start with something really, really simple, then
build up from it as you see how it works. It is very hard
for me to work out what you might be up to - the looping
problem, for example, was a very, very long shot!

Create the batch file c:\Test1.bat and place these lines inside:
@echo off
echo %date% %time% >> c:\test1.txt

Use the Task Scheduler to run this batch file, then examine
the file c:\test1.txt. I fully expect it to show a date/time
stamp.

Now modify ***one thing at a time** and keep testing
it until you are back at your own batch file. If you do
this carefully and test it consistently then you will find
out where you go wrong.
 
Back
Top