繁体   English   中英

Windows启动时通过Win32com进行Python任务计划程序

[英]Python task scheduler via win32com on windows startup

大家好,我正在尝试通过python文件在Windows 10上的任务计划程序中设置新任务,我在这里在stackoverflow和dzone上找到了如何针对每日触发器或登录触发器进行操作,但是如果我想修改到启动触发器,一切都根本不起作用,根据此处找到的微软网站,我将触发器号更改为8,但这给了我错误

文件“ C:/ Users / PTV4CLJ / Desktop / Python scripturi / test searcher / task.py”,行85,在操作中。ID= action_id文件“ C:\\ Python27 \\ lib \\ site-packages \\ win32com \\ client__init __。py”行481,在setattr中引发AttributeError(“'%s'对象没有属性'%s'”%(repr(self),attr))AttributeError:''对象没有属性'ID'

有人可以帮我吗?我想将它设置为从两个小时运行到两个小时。

这是我使用的代码

 import win32com.client

computer_name = "" #leave all blank for current computer, current user

computer_username = ""

computer_userdomain = ""

computer_password = ""

action_id = "Test Task" #arbitrary action ID

action_path = r"c:\windows\system32\calc.exe" #executable path (could be python.exe)

action_arguments = r'' #arguments (could be something.py)

action_workdir = r"c:\windows\system32" #working directory for action executable

author = "Someone" #so that end users know who you are

description = "testing task" #so that end users can identify the task

task_id = "Test Task"

task_hidden = False #set this to True to hide the task in the interface

username = ""

password = ""

run_flags = "TASK_RUN_NO_FLAGS" #see dict below, use in combo with username/password

#define constants

TASK_TRIGGER_STARTUP = 8

TASK_CREATE = 2

TASK_CREATE_OR_UPDATE = 6

TASK_ACTION_EXEC = 0

IID_ITask = "{148BD524-A2AB-11CE-B11F-00AA00530503}"

RUNFLAGSENUM = {

    "TASK_RUN_NO_FLAGS"              : 0,

    "TASK_RUN_AS_SELF"               : 1,

    "TASK_RUN_IGNORE_CONSTRAINTS"    : 2,

    "TASK_RUN_USE_SESSION_ID"        : 4,

    "TASK_RUN_USER_SID"              : 8

}

#connect to the scheduler (Vista/Server 2008 and above only)

scheduler = win32com.client.Dispatch("Schedule.Service")

scheduler.Connect(computer_name or None, computer_username or None, computer_userdomain or None, computer_password or None)

rootFolder = scheduler.GetFolder("\\")

#(re)define the task

taskDef = scheduler.NewTask(0)

colTriggers = taskDef.Triggers

trigger = colTriggers.Create(TASK_TRIGGER_STARTUP)


trigger.StartBoundary = "2019-09-19T08:00:00"#never start
trigger.EndBoundary="2019-09-20T08:00:00"

trigger.Enabled = False

colActions = taskDef.Actions

action = colActions.Create(TASK_ACTION_EXEC)

action.ID = action_id

action.Path = action_path

action.WorkingDirectory = action_workdir

action.Arguments = action_arguments

info = taskDef.RegistrationInfo

info.Author = author

info.Description = description

settings = taskDef.Settings

settings.Enabled = False

settings.Hidden = task_hidden

#register the task (create or update, just keep the task name the same)

result = rootFolder.RegisterTaskDefinition(task_id, taskDef, TASK_CREATE_OR_UPDATE, "", "", RUNFLAGSENUM[run_flags] ) #username, password

#run the task once

task = rootFolder.GetTask(task_id)

task.Enabled = True

runningTask = task.Run("")

task.Enabled = False

我将做出一个疯狂的猜测,并说它应该像这样:

action.Id = action_id

请注意,所有其他字段均为驼峰式。

看起来IAction拥有put_Id方法,并且我假设Python包装器保留了作为属性公开的字段的大小写。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM