Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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%"
)
share|improve this question
1  
You 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 Oct 2 '12 at 8:25

2 Answers 2

up vote 3 down vote accepted
+50

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
share|improve this answer
    
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 Oct 5 '12 at 14:20
    
@Rogier: I wrote a new answer. –  Scott Oct 15 '12 at 22:08

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.