0

i have used below code from Python check for Completed and failed Task Windows scheduler link . i am able to find the disabled schedulers, but not able to start the disabled scheduler.

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)`enter code here`

For example:

print('State      : %s' % TASK_STATE[task.State])

if TASK_STATE[task.State] == "Disabled":
    task_track_started = True

i tried above code is not working to start the service. Please help, thanks in advance.

sreenid
  • 53
  • 1
  • 1
  • 4

1 Answers1

0

i found the solution after spending 2 hours of time.

 if TASK_STATE[task.State] == "Disabled":
    print("Task Status is Disabled...")
    print(task.Path)
    p = subprocess.Popen(['SCHTASKS', '/CHANGE', '/TN', task.Path,"/ENABLE"],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    print("Task status is ENABLED....")
sreenid
  • 53
  • 1
  • 1
  • 4