简体   繁体   English

通过Python运行Windows任务计划程序

[英]Running Windows Task Scheduler through Python

I am new to SO. 我是SO的新手。 I am given the task of creating a windows task schedule to run a .BAT file using our Python API to push it to our fleet of remote PCs. 我的任务是创建一个Windows任务计划,使用我们的Python API运行.BAT文件,将其推送到我们的远程PC机群。

I am having issues with passing the arguments over to the schtask.exe . 我将问题传递给schtask.exe时遇到问题。

Here is the code: 这是代码:

import subprocess
path = "c:\windows\System32\schtasks.exe"
subprocess.Popen([path, "schtasks /create /SC ONLOGON /TN 'Update_Automation_Beta' /TR 'C:\test\run_admin.bat'"], shell = True)

Note: The task is just a test task right now, while I try to figure it out. 注意:这个任务现在只是一个测试任务,而我试图解决它。 Also if typed directly into command prompt window, it will work, removing the quotes etc. 此外,如果直接输入命令提示符窗口,它将工作,删除引号等。

This worked for me: 这对我有用:

import subprocess
subprocess.call('schtasks /create /SC ONLOGON /TN "Update_Automation_Beta" /TR "C:\test\run_admin.bat"')

Use single quotes outside and double qoutes inside. 在内部使用单引号,在内部使用双qoutes。 Also you can put in the full path to schtasks, if you need. 如果需要,您还可以完整地使用schtasks。

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

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