本文介绍了有没有办法通过python 3向windows任务调度程序添加任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个生成 GUI 的主要 python 脚本,我希望用户能够通过该 GUI 创建、修改和删除由 Windows 任务调度程序管理的计划.
I have a main python script that generates a GUI, and through that GUI I want the user to be able to create, amend, and delete schedules managed by the windows task scheduler.
推荐答案
此代码创建了一个将在 5 分钟内运行的任务(使用 pywin32):
This code creates a task which will run in 5 minutes (uses pywin32):
import datetime
import win32com.client
scheduler = win32com.client.Dispatch('Schedule.Service')
scheduler.Connect()
root_folder = scheduler.GetFolder('\\')
task_def = scheduler.NewTask(0)
# Create trigger
start_time = datetime.datetime.now() + datetime.timedelta(minutes=5)
TASK_TRIGGER_TIME = 1
trigger = task_def.Triggers.Create(TASK_TRIGGER_TIME)
trigger.StartBoundary = start_time.isoformat()
# Create action
TASK_ACTION_EXEC = 0
action = task_def.Actions.Create(TASK_ACTION_EXEC)
action.ID = 'DO NOTHING'
action.Path = 'cmd.exe'
action.Arguments = '/c "exit"'
# Set parameters
task_def.RegistrationInfo.Description = 'Test Task'
task_def.Settings.Enabled = True
task_def.Settings.StopIfGoingOnBatteries = False
# Register task
# If task already exists, it will be updated
TASK_CREATE_OR_UPDATE = 6
TASK_LOGON_NONE = 0
root_folder.RegisterTaskDefinition(
'Test Task', # Task name
task_def,
TASK_CREATE_OR_UPDATE,
'', # No user
'', # No password
TASK_LOGON_NONE)
有关任务及其属性的更多信息,请访问:https://docs.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-objects
More info on tasks and their properties here: https://docs.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-objects
这篇关于有没有办法通过python 3向windows任务调度程序添加任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!