2

With PowerShell, I want to create a new task that starts a Python script:

enter image description here

Setting it this way via the GUI Task Scheduler works, but in my PowerShell code below, I don't know how to specify the "add arguments" field:

$action = New-ScheduledTaskAction -Execute "C:\Program Files\Python\Python38\python.exe"
$trigger = New-ScheduledTaskTrigger -Daily -At 5:00am
$trigger.StartBoundary = [DateTime]::Parse($trigger.StartBoundary).ToLocalTime().ToString("s")
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0

Register-ScheduledTask -Action $action -Trigger $trigger -Settings $settings -TaskName "Full Computer Backup" -Description "Backs up computer"

What change must I make to the PowerShell code to add the argument (in this case, the path to the python script [.py file])?

2 Answers 2

2

Use the -Argument parameter with the New-ScheduledTaskAction command.

You can add the full path as the value for the -Argument parameter (e.g. -Argument "<script path value>") to add this as the value for the task's Add arguments (optional) field value.

PowerShell

$action = New-ScheduledTaskAction -Execute "C:\Program Files\Python\Python38\python.exe" -Argument "C:\Scripts\mypScript.py"
$trigger = New-ScheduledTaskTrigger -Daily -At 5:00am
$trigger.StartBoundary = [DateTime]::Parse($trigger.StartBoundary).ToLocalTime().ToString("s")
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0

Register-ScheduledTask -Action $action -Trigger $trigger -Settings $settings -TaskName "Full Computer Backup" -Description "Backs up computer"

Supporting Resources

1
  • 1
    The solution was that I had accidentally copied a different kind of dash (as opposed to the hyphen key on the keyboard) so PowerShell was giving me that error. I do have access to the "Argument" parameter. Commented Apr 18, 2021 at 16:28
0

I encountered the same problem: Windows Server 2012r2 task scheduler was running powershell scripts which in turn were supposed to start python scripts. Powershell code seemed to run but not the python scripts.

Additional Powershell argument -ExecutionPolicy Bypass did not help me, but adding the folder where all scripts are located to "start in" field solved the issue.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 10, 2022 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .