6

I am new to 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.

I am having issues with passing the arguments over to the 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.

3
  • If you want to use a list, split the entire command line into separate arguments, but don't repeat schtasks. And don't use shell=True, since there's no reason to run this using cmd.exe. Commented May 26, 2015 at 14:45
  • Thank you, I did figure it out by doing the following: - parsing the code using shlex.split() which made it out to be something like this - '/delete', '/tn', 'update_auto_beta', '/f' - Removing the schtasks thank you Commented May 26, 2015 at 15:15
  • The command line must already be quoted properly if shlex.split works. On Windows Popen has to rebuild the list it into a string for CreateProcess, so just pass the command as a string. Commented May 26, 2015 at 15:28

1 Answer 1

2

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. Also you can put in the full path to schtasks, if you need.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.