Link to home
Start Free TrialLog in
Avatar of rakkad
rakkadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

DOS batch script report windows schedule task

Hi

I would like a batch script that will check the status of a windows scheduler task to do the following:-

- Starting Schedule Task

1. Report if the schedule task failed to run
2. Report if the schedule task successfully run

- Status of the Schedule task

3. Report if the schedule task failed
4. Report  if the schedule task completed successfully

- Status of the schedule completion job

5. Report if the 'running' status is completed successfully and still not running
Avatar of Colin_UK
Colin_UK
Flag of United Kingdom of Great Britain and Northern Ireland image

Funny enough I'm doing similar at the moment.
You can get the results using the schtasks.exe cli tool provided by Microsoft.

To be able to use the results progmatically I'd suggest using the /FO switch and select LIST mode, then it will be easier to tokenise the results for use later.

Full details available here.
https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx

Hope this helps
Colin
Avatar of rakkad

ASKER

I need a way to find if a schedule task has failed with an error code
schtasks does give error info if the task could not be started. ie:
Folder: \Microsoft\Windows\Customer Experience Improvement Program
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
Consolidator                             04/05/2017 03:00:00    Could not start
KernelCeipTask                           04/05/2017 03:30:00    Ready
UsbCeip                                  05/05/2017 01:30:00    Ready

Personally I would question if this is the correct place to be looking for failures.
If you want to know if the task executed with an error it may be better to use the %ERRORLEVEL% environment variable after the task and report accordingly. Even if the task failed, the action of the scheduler could well report a succesful launch.
Avatar of rakkad

ASKER

I want to report out if the "Last Result" does not equal to "0"
Sorry, I'm not sure what you mean.
If you are refering to the exit code of the last executed command you can get that from %ERRORLEVEL%, returns 0 for success and 1 for failure.
Avatar of Shaun Vermaak
Personally, I would use blat or Powershell and send mail when process failed. What do these scheduled tasks do?
Avatar of rakkad

ASKER

Is there a way to report if a windows scheduler task is still running ?
schtasks.exe reports running tasks under the Status field as "Running"
Avatar of rakkad

ASKER

I have run the following command to obtain the Last Result

schtasks.exe /query /fo list /v /TN %TASKNAME% | find "Last Result"

in this case the schedule task has failed with an error code of  -21454535

which is correct as I am trying to detect error codes and output the result.

So,  what I want to detect in the script is if the "Last Result" does not equal to 0 then output the result as an error
"if errorlevel" can be used within batch files, can be called whenever, and it always relates to the last command executed.

so the command
if errorlevel goto unload

Open in new window


will send execution to the label 'unload' only if the errorlevel (returned by the last executed command) is anything other than 0.
Avatar of rakkad

ASKER

I am confused to your statement.  I simply want to detect that the script has failed because the "Last Result" was not equal to 0
The issue you will have is differentiating what exactly has failed.

If a script executes 10 commands before it exits then it is the script that will need to modify the errorlevel value. All 10 of the commands could have failed, but that doesn't mean the script has failed, so most likely to return 0 (ie: success).

I don't think I can really help any more than I have unless you provide details of what you are trying to achieve.
Could you do that?

Colin
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial