Skip to content Skip to sidebar Skip to footer

Python Check For Completed And Failed Task Windows Scheduler

Does anyone know of a way or a resource I can look at to be able to check the status of all my Windows tasks I have in the task scheduler? I would like to see if that see if the ta

Solution 1:

The following uses the Task Scheduler API to print basic information for all registered tasks, including the last run time and result.

import win32com.client

TASK_ENUM_HIDDEN = 1
TASK_STATE = {0: 'Unknown',
              1: 'Disabled',
              2: 'Queued',
              3: 'Ready',
              4: 'Running'}

scheduler = win32com.client.Dispatch('Schedule.Service')
scheduler.Connect()

n = 0
folders = [scheduler.GetFolder('\\')]
while folders:
    folder = folders.pop(0)
    folders += list(folder.GetFolders(0))
    tasks = list(folder.GetTasks(TASK_ENUM_HIDDEN))
    n += len(tasks)
    for task in tasks:
        settings = task.Definition.Settings
        print('Path       : %s' % task.Path)
        print('Hidden     : %s' % settings.Hidden)
        print('State      : %s' % TASK_STATE[task.State])
        print('Last Run   : %s' % task.LastRunTime)
        print('Last Result: %s\n' % task.LastTaskResult)
print('Listed %d tasks.' % n)

This starts with only the root folder in the list. Each pass through the loop pops a folder; pushes all of its subfolders; and lists the tasks in the folder. It continues until the list of folders is empty.

COM Interfaces

Alternatively, here's a walk_tasks generator that's modeled on the standard library's os.walk.

import os
import pywintypes
import win32com.client

TASK_ENUM_HIDDEN = 1
TASK_STATE = {
    0: 'Unknown',
    1: 'Disabled',
    2: 'Queued',
    3: 'Ready',
    4: 'Running'
}

defwalk_tasks(top, topdown=True, onerror=None, include_hidden=True,
               serverName=None, user=None, domain=None, password=None):
    scheduler = win32com.client.Dispatch('Schedule.Service')
    scheduler.Connect(serverName, user, domain, password)
    ifisinstance(top, bytes):
        ifhasattr(os, 'fsdecode'):
            top = os.fsdecode(top)
        else:
            top = top.decode('mbcs')
    ifu'/'in top:
        top = top.replace(u'/', u'\\')
    include_hidden = TASK_ENUM_HIDDEN if include_hidden else0try:
        top = scheduler.GetFolder(top)
    except pywintypes.com_error:
        if onerror isnotNone:
            onerror(error)
        returnfor entry in _walk_tasks_internal(top, topdown, onerror, include_hidden):
        yield entry


def_walk_tasks_internal(top, topdown, onerror, flags):
    try:
        folders = list(top.GetFolders(0))
        tasks = list(top.GetTasks(flags))
    except pywintypes.com_error as error:
        if onerror isnotNone:
            onerror(error)
        returnifnot topdown:
        for d in folders:
            for entry in _walk_tasks_internal(d, topdown, onerror, flags):
                yield entry

    yield top, folders, tasks

    if topdown:
        for d in folders:
            for entry in _walk_tasks_internal(d, topdown, onerror, flags):
                yield entry

Example

if __name__ == '__main__':
    n = 0for folder, subfolders, tasks in walk_tasks('/'):
        n += len(tasks)
        for task in tasks:
            settings = task.Definition.Settings
            print('Path       : %s' % task.Path)
            print('Hidden     : %s' % settings.Hidden)
            print('State      : %s' % TASK_STATE[task.State])
            print('Last Run   : %s' % task.LastRunTime)
            print('Last Result: %s\n' % task.LastTaskResult)
    print('Listed %d tasks.' % n)

Solution 2:

task scheduler can be accessed from command line using schtasks and at

schtasks: https://technet.microsoft.com/en-us/library/cc772785%28v=ws.10%29.aspx

at: https://technet.microsoft.com/en-us/library/cc755618%28v=ws.10%29.aspx

run schtasks /query from python using subprocess.check_output see Running windows shell commands with python

https://technet.microsoft.com/en-us/library/cc722006.aspx

The tasklist command lists all running programs and services or in powershell get-process

https://superuser.com/questions/914782/how-do-you-list-all-processes-on-the-command-line-in-windows

Post a Comment for "Python Check For Completed And Failed Task Windows Scheduler"