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 task failed or was successful. I would like to do this in Python.
I have looked a little at using the win32com.client module. I can see what tasks are but can't find what the status of completed job are.
import win32com.client
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
tasks = scheduler.GetRunningTasks(1)
names = [tasks.Item(i+1).Name for i in range(tasks.Count)]
print names
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
ITaskService
ITaskFolder
IRegisteredTask
ITaskDefinition
ITaskSettings
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'
}
def walk_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)
if isinstance(top, bytes):
if hasattr(os, 'fsdecode'):
top = os.fsdecode(top)
else:
top = top.decode('mbcs')
if u'/' in top:
top = top.replace(u'/', u'\\')
include_hidden = TASK_ENUM_HIDDEN if include_hidden else 0
try:
top = scheduler.GetFolder(top)
except pywintypes.com_error:
if onerror is not None:
onerror(error)
return
for 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 is not None:
onerror(error)
return
if not 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 = 0
for 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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With