Reputation: 41
I need to know if the task ran successfully so I can create an event in the Application log saying so. Is there a way to get this in code? I tried the following:
echo ErrorLevel of "c:\windows\system32\tasks\my task" = %ErrorLevel%
But I get 0 every time, even if I stop it prematurely (0x41306) or while the task is still running (should be 0x41301). Does anyone have any ideas? Thank you.
I found a workaround to this. Instead of getting the exit code of the task, I got the exit code of the batch script that actually runs and if it's anything but 0 then I make an error application event, otherwise it's a success application event.
Upvotes: 4
Views: 11428
Reputation:
The following batch file accepts a parameter of a task name, for instance if you named the bat file "getresult.bat" you would call "getresult GoogleUpdateTaskMachineCore" (If the name has spaces, put quotes around it).
This is very verbose, so let me know if you need help adapting it to fit your needs.
Tested and working in Windows 8, I believe it should work for XP/Vista/7 as well.
@ECHO OFF
IF %1=="" GOTO EXITNOINPUT
ECHO Checking Tasks for "%1"...
FOR /F "tokens=2delims=:" %%I IN ('schtasks /tn %1 /fo LIST /v ^| FIND "Last Result"') DO (
SET result=%%I
)
IF NOT DEFINED result GOTO EXITNOTFOUND
ECHO Done...
ECHO The Last Result Was: %result%
GOTO EXITNORMAL
:EXITNOTFOUND
echo The scheduled task was not found.
GOTO EXITNORMAL
:EXITNOINPUT
echo You must provide a query. (getresult servicename)
GOTO EXITNORMAL
:EXITNORMAL
Upvotes: 0