问题描述
我正在尝试创建一个每次当前用户登录时都会运行的任务.
I’m trying to create a task that will run each time the current user logs in.
我找不到任何好的示例或文档.我发现的最好的例子是this.
I’m having trouble finding any good examples or documentation. The best example I’ve found is this.
我尝试将 TASK_TRIGGER_DAILY = 2
换成 TASK_TRIGGER_LOGON = 9
,删除 tigger.DaysInterval = 100
并使用相关的对象,如 >trigger.Delay
等在这里找到.
I tried swapping out TASK_TRIGGER_DAILY = 2
for TASK_TRIGGER_LOGON = 9
, removing tigger.DaysInterval = 100
and using the associated objects like trigger.Delay
, etc. found here.
它总是导致:
Pywintyps.com error: (-2147352567, ‘Exception occurred.’, (0, None, None, None, 0 -2147024809), None) on line 67: result = rootFolder.RegisterTaskDefinition(task_id, taskDef, TASK_CREATE_OR_UPDATE, "", "", RUNFLAGSENUM[run_flags] )
#username, password
调用 schtasks.exe
有效,但这需要 UAC 提升请求,我想避免这种情况.
Calling schtasks.exe
works, but that requires a UAC elevation request which I’d like to avoid.
一个工作示例或一些相关的 Python 文档会很棒.我对 C++
不够精通,无法将其全部转换为 Python
.
A working example or some relevant Python documentation would be great. I’m not well versed enough in C++
to translate it all over to Python
.
推荐答案
# Uses the COM Task Scheduler Interface to create a task
# scheduled to execute when the current user logs on.
import win32com.client
import os
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 = "Run calc.exe when the current user logs on"
task_id = "Test Task"
task_hidden = False #set this to True to hide the task in the interface
username = ""
password = ""
#define constants
TASK_TRIGGER_LOGON = 9
TASK_CREATE_OR_UPDATE = 6
TASK_ACTION_EXEC = 0
TASK_LOGON_INTERACTIVE_TOKEN = 3
#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_LOGON)
trigger.Id = "LogonTriggerId"
trigger.UserId = os.environ.get('USERNAME') # current user account
#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
#settings.StartWhenAvailable = True
#register the task (create or update, just keep the task name the same)
result = rootFolder.RegisterTaskDefinition(task_id, taskDef, TASK_CREATE_OR_UPDATE, "", "", TASK_LOGON_INTERACTIVE_TOKEN)
'''
# run the task once
task = rootFolder.GetTask(task_id)
task.Enabled = True
runningTask = task.Run("")
task.Enabled = False
'''
最难的部分是弄清楚 TASK_LOGON_INTERACTIVE_TOKEN = 3
而不是 4,因为它在 microsoft 文档中显示 example.在这个 post.
The hardest part was figuring out TASK_LOGON_INTERACTIVE_TOKEN = 3
and not 4 as it's shown in the microsoft documentation example. Found it in this post.
这篇关于在python中使用TASK_TRIGGER_LOGON创建计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!