Set scheduled task last result to 0x0 manually

1

Every night a task runs that checks if any scheduled task has a Last Result is not equal to 0x0. If a scheduled tasks has an error like 0x1, then automatically an e-mail is sent to me. As some tasks are running only weekly, and sometimes an error occurs which results in not equal to 0x0, every night an e-mail is sent with the error message, as the Last Result column still shows the last result of 0x1. But I would like to set the Last Result column to 0x0 manually if I solved a problem, so I won't get every night an e-mail with the error message.

So is it possible to set the scheduled tasks Last Result to 0x0 manually (or by a script)?

@harrymc. See located script underneath that is sending the e-mail. I can easily add a criteria to ignore result 0x1 (or another code), however this is not the solution as most of the times this result is a real error and has to be e-mailed.

set YourEmailAddress=to@email.com
set SMTPServer=SMTPserver
set PathToScript=c:\scripts
set FromAddress=from@email.com

for /F "delims=" %%a in ('schtasks /query /v /fo:list ^| findstr /i "Taskname Result"') do call :Sub %%a
goto :eof

:Sub
set Line=%*
set BOL=%Line:~0,4%
set MOL=%Line:~38%
if /i %BOL%==Task (
set name=%MOL%
goto :eof
)
set result=%MOL%
echo Task Name=%name%, Task Result=%result%
if not %result%==0 (
echo Task %name% failed with result %result% > %PathToScript%\taskcheckerlog.txt
bmail %PathToScript%\taskcheckerlog.txt -t %YourEmailAddress% -a "Warning! Failed %name% Scheduled Task on %computername%" -s %SMTPServer% -f %FromAddress% -b "Task %name% failed with result %result% on CorVu scheduler %computername%"
)

Rogier

Posted 2012-06-25T10:06:36.203

Reputation: 145

1You could locate the scheduled job that sends you these emails, then modify its criteria to ignore 0x1, or even disable it. If you can locate this job, please add these details into the post. – harrymc – 2012-10-02T08:25:53.650

Answers

3

Here’s a solution to your problem, although it’s not an answer to your question.  You want to see only tasks that ran today and have a non-zero status, right?

On Windows 7, schtasks /query reports dates in n/n/nnnn format (without leading zeroes) while the date command uses nn/nn/nnnn (with leading zeroes).  So, to get today’s date in n/n/nnnn format, I would do

set today_temp1=/%date:~4%
set today_temp2=%today_temp1:/0=/%
set today=%today_temp2:~1%

If schtasks and date are compatible on XP, then you can forgo the above.  If you know a cleaner way to strip leading zeroes off dates, please tell me.

Then change your findstr to findstr "TaskName Last", so you get TaskName, Last Run Time, and Last Result.  And change your Sub to:

:Sub
set Line=%*
set BOL=%Line:~0,8%
set MOL=%Line:~38%
if /i "%BOL%"=="TaskName" (
    set name=%MOL%
    goto :eof
)
if /i "%BOL%"=="Last Run" (
    REM Break date and time apart.
    call :Sub2 %MOL%
    goto :eof
)
set result=%MOL%
echo Task Name=%name%, Last Run=%lastrun%, Task Result=%result%
if not %result%==0 (
    if %lastrun%==%today% (
        ︙

goto :eof


:Sub2
set lastrun=%1
REM The Last Run time is %2.
goto :eof

Scott

Posted 2012-06-25T10:06:36.203

Reputation: 17 653

This is a very useful answer, however tasks can also start one day before, or in another words, the error may be shown one day before in the scheduled tasks. I will test your answer and see what the result is. – Rogier – 2012-10-05T14:20:46.580

@Rogier: I wrote a new answer. – Scott – 2012-10-15T22:08:30.700

0

Here’s a solution to your problem, although it’s not an answer to your question.  You want to see only tasks that ran and failed since the last time you looked, right?  So, keep a log of what task failures have been reported.

Create empty files reported0.txt and reported0.txt.  (Obviously, you can change the names if you want.)

set YourEmailAddress, SMTPServer, PathToScript, and FromAddress as appropriate.

del reported0.txt               > nul
ren reported1.txt reported0.txt > nul

for /F "delims=" %%a in ('schtasks /query /v /fo:list ^
                                            ^| findstr "TaskName Last"') do (
        call :Sub %%a
)
exit /b


:Sub
set Line=%*
set BOL=%Line:~0,8%
set MOL=%Line:~38%
if "%BOL%"=="TaskName" (
        set name=%MOL%
        exit /b
)
if "%BOL%"=="Last Run" (
        set run_date_time=%MOL%
        REM
Break date and time apart if you want.
        exit /b
)
set result=%MOL%
if not %result%==0 (
        set pattern_string=@%name%@%run_date_time%@%result%@
        find "%pattern_string%" reported0.txt > nul
        if errorlevel 1 (
                REM error => not found in file => not previously reported.
                ︙           (
Report task failure via email.)
        )
        echo %pattern_string% >> reported1.txt
)
exit /b

Scott

Posted 2012-06-25T10:06:36.203

Reputation: 17 653