Task Scheduler Task returns 0x1 for Batch File
In case you run a batch script by using a scheduled task in windows and the script itself returns an error which can be ignored, you can manipulate the returned error level (code) from that script by hand.
The following clear IIS Logs batch file will clear all IIS logs older than 14 days.
@echo off forfiles /p "C:inetpublogsLogFiles" /s /m *.* /c "cmd /c Del @path" /d -14
Source: https://docs.microsoft.com/de-de/windows-server/administration/windows-commands/forfiles
If the script runs and no logs older than 14 days will be found, it returns the following error.
In this case also the scheduled task will note that last run result with 0x1 for Incorrect function called or unknown function called.
- 0 or 0x0: The operation completed successfully.
- 1 or 0x1: Incorrect function called or unknown function called.
In case only the script itself returns an error and not the scheduled task like for missing permissions, you can manipulate the returned error level from the script, or if possible the better solution, avoid that the script will run into an unhandled exception.
set ERRORLEVEL=0
or
exit /b 0
@echo off forfiles /p "C:inetpublogsLogFiles" /s /m *.* /c "cmd /c Del @path" /d -14 set ERRORLEVEL=0
@echo off forfiles /p "C:inetpublogsLogFiles" /s /m *.* /c "cmd /c Del @path" /d -14 exit /b 0
You can test both variants by using the following command at the end.
echo Error Level: %errorlevel%
Now the result from the script will always be 0 or 0x0: The operation completed successfully.
The variant by using exit /b 0 explained.
One last remark about the @echo off command in batch files.
By using the @ symbol in front of each command line, only the result will displayed in the output. Without the @ symbol in front, it will first display the command itself and then the result in the output.
In order you do not have to put the @ symbol in front of each line in your script, you can use the @echo off command on top of the script to suppress all command lines in the output and only return the result.
Links
Result Codes
https://en.wikipedia.org/wiki/Windows_Task_Scheduler#Column_’Last_Result’
forfiles
https://docs.microsoft.com/de-de/windows-server/administration/windows-commands/forfiles