python 建立windows计划任务
After looking around the internet for many days. I've decided to ask my own question. I've done some digging around and found some ways to implement this but i'm stuck. I know that i have to change the logon_type and set the username but it doesn't seem to work. I've tried using the salt.module.win_task.py as a guideline but it still doesn't work for me. This is my code (borrowed from someone else with some tweaks i did)
main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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 = 30 ) 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 = "TEST" action.Path = "C:/test/test.bat" action.Arguments = '' action.WorkingDirectory = "C:/test/" # Set parameters task_def.RegistrationInfo.Description = 'Run test.bat' task_def.Settings.Enabled = True task_def.Settings.StopIfGoingOnBatteries = False task_def.Settings.Hidden = False task_def.Settings.startwhenavailable = True task_def.Settings.DisallowStartIfOnBatteries = 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 name task_def, TASK_CREATE_OR_UPDATE, '', # No user '', # No password TASK_LOGON_NONE) |
I've tried adding (copying from win_task.py)
1 2 3 4 5 6 7 | TASK_RUNLEVEL_HIGHEST = 1 TASK_LOGON_SERVICE_ACCOUNT = 5 task_def.Principal.UserID = "SYSTEM" task_def.Principal.DisplayName = "SYSTEM" task_def.Principal.GroupID = "Administrators" task_def.Principal.LogonType = TASK_LOGON_SERVICE_ACCOUNT task_def.Principal.RunLevel = TASK_RUNLEVEL_HIGHEST |
and changing this part
1 2 3 4 5 6 7 | root_folder.RegisterTaskDefinition( "TEST" , # Task name task_def, TASK_CREATE_OR_UPDATE, task_def.Principal.UserID, None , # No password TASK_LOGON_SERVICE_ACCOUNT) |
answer:
root_folder.RegisterTaskDefinition( File "<COMObject GetFolder>", line 3, in RegisterTaskDefinition pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024891), None)
answer:
Your code works great! The reason you are getting the error code: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024891), None)
is because you are trying to change Principal.Runlevel
without administrative privileges.
For your code to work, Run a cmd window as Admin and use that to run your win_task.py. >python win_task.py
Here is a video on how to do that!
I was able to create a task and change the "run with highest privileges" checkbox after I ran the script as admin.
Here is the only resource I was able to find explaining principal.runlevel
url : https://docs.microsoft.com/en-us/windows/win32/taskschd/principal-runlevel
reply: I've tried to run it with ADM privileges but still get the same error. Is there any other option that you know?
reply: A possible reason for this error is that you may be an admin on your system but with specific tasks (common in work places). If you are working on a personal computer this shouldn't be the case. Try using the exact code above and see if you get the same results. Some python libraries are know for throwing theses errors but they have no immediate fix at the moment.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下