I am trying to list all scheduled tasks on Windows with a Python script.
On this post, Python check for Completed and failed Task Windows scheduler there is some code which worked.
import win32com.client
TASK_STATE = {0: 'Unknown',
1: 'Disabled',
2: 'Queued',
3: 'Ready',
4: 'Running'}
scheduler = win32com.client.Dispatch('Schedule.Service')
scheduler.Connect()
folders = [scheduler.GetFolder('\\')]
while folders:
folder = folders.pop(0)
folders += list(folder.GetFolders(0))
for task in folder.GetTasks(0):
print('Path : %s' % task.Path)
print('State : %s' % TASK_STATE[task.State])
print('Last Run : %s' % task.LastRunTime)
print('Last Result: %s\n' % task.LastTaskResult)
However, I would like to display the path of the file it executes (e.g. c:\test\aaa.bat
) and the parameters of the command. How could this be done?
And could anyone explain the meaning of some commands in the code:
- [] in the code "[scheduler.GetFolder('\')]"
- Where could I get the detail explaination of code like list(folder.GetFolders(0)), folder.GetTasks(0)? Is there any documentation available?
Dispatch
returns anITaskService
instance.GetFolder('\\')
returns anITaskFolder
for the root folder in the scheduler namespace.GetFolders(0)
returns the subfolders in anITaskFolderCollection
.GetTasks(0)
gets the non-hidden tasks in the folder in anIRegisteredTaskCollection
. To include hidden tasks, useGetTasks(TASK_ENUM_HIDDEN)
, i.e.GetTasks(1)
. Enumerating this returns the task as anIRegisteredTask
. Note that itsDefinition
property is anITaskDefinition
. – Smallishos.walk
style generator function that yields the path, subfolders and tasks in each folder. – Smallishwalk_tasks
generator function to my answer on the linked question. – Smallish