test_addtask_2.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import pythoncom, time, win32api
  2. from win32com.taskscheduler import taskscheduler
  3. task_name='test_addtask_2.job'
  4. ts=pythoncom.CoCreateInstance(taskscheduler.CLSID_CTaskScheduler,None,
  5. pythoncom.CLSCTX_INPROC_SERVER,taskscheduler.IID_ITaskScheduler)
  6. tasks=ts.Enum()
  7. for task in tasks:
  8. print task
  9. if task_name in tasks:
  10. print 'Deleting existing task '+task_name
  11. ts.Delete(task_name)
  12. t=ts.NewWorkItem(task_name)
  13. t.SetComment('Test a task running as local system acct')
  14. t.SetApplicationName('c:\\python23\\python.exe')
  15. t.SetPriority(taskscheduler.REALTIME_PRIORITY_CLASS)
  16. t.SetParameters('test_localsystem.py')
  17. t.SetWorkingDirectory('c:\\python23')
  18. t.SetCreator('test_addtask_2.py')
  19. t.SetMaxRunTime(20000) #milliseconds
  20. t.SetFlags(taskscheduler.TASK_FLAG_DELETE_WHEN_DONE)
  21. t.SetAccountInformation('',None) ## empty string for account name means to use local system
  22. ## None is only valid for local system acct or if task flags contain TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
  23. run_time = time.localtime(time.time() + 60)
  24. tr_ind, tr=t.CreateTrigger()
  25. tt=tr.GetTrigger()
  26. tt.Flags=0 ## flags for a new trigger default to TASK_TRIGGER_FLAG_DISABLED (4), make sure to clear them if not using any
  27. tt.TriggerType=taskscheduler.TASK_TIME_TRIGGER_ONCE
  28. tt.BeginYear=int(time.strftime('%Y',run_time))
  29. tt.BeginMonth=int(time.strftime('%m',run_time))
  30. tt.BeginDay=int(time.strftime('%d',run_time))
  31. tt.StartMinute=int(time.strftime('%M',run_time))
  32. tt.StartHour=int(time.strftime('%H',run_time))
  33. tr.SetTrigger(tt)
  34. print t.GetTriggerString(tr_ind)
  35. pf=t.QueryInterface(pythoncom.IID_IPersistFile)
  36. pf.Save(None,1)