def run_wait(name, location='\\'):
 if (name not in list_tasks(location)):
  return '{0} not found in {1}'.format(name, location)
 pythoncom.CoInitialize()
 task_service = win32com.client.Dispatch('Schedule.Service')
 task_service.Connect()
 task_folder = task_service.GetFolder(location)
 task = task_folder.GetTask(name)
 if (task.State == TASK_STATE_RUNNING):
  return 'Task already running'
 try:
  task.Run('')
  time.sleep(1)
  running = True
 except pythoncom.com_error:
  return False
 while running:
  running = False
  try:
   running_tasks = task_service.GetRunningTasks(0)
   if running_tasks.Count:
    for item in running_tasks:
     if (item.Name == name):
      running = True
  except pythoncom.com_error:
   running = False
 return True

def run_wait ( name , location = '\\' ) : if ( name not in list_tasks ( location ) ) : return '{0} not found in {1}' . format ( name , location ) pythoncom . CoInitialize ( ) task_service = win32com . client . Dispatch ( 'Schedule.Service' ) task_service . Connect ( ) task_folder = task_service . GetFolder ( location ) task = task_folder . GetTask ( name ) if ( task . State == TASK_STATE_RUNNING ) : return 'Task already running' try : task . Run ( '' ) time . sleep ( 1 ) running = True except pythoncom . com_error : return False while running : running = False try : running_tasks = task_service . GetRunningTasks ( 0 ) if running_tasks . Count : for item in running_tasks : if ( item . Name == name ) : running = True except pythoncom . com_error : running = False return True
run a scheduled task and return when the task finishes .