CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Scheduled Task Creation and Parameter Passing for Scripts

👀 Views: 54 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-16
powershell scheduled-tasks task-scheduler PowerShell

I can't seem to get Quick question that's been bugging me - I've searched everywhere and can't find a clear answer... I'm running into problems when trying to create a scheduled task in PowerShell 7.3 that runs a script with parameters. I've crafted a script that should execute without issues, but when it's invoked through the Task Scheduler, it fails to pass the parameters correctly. I've tried using both the `New-ScheduledTask` and `Register-ScheduledTask` cmdlets, but it seems like the parameters are being ignored. Here's the relevant part of my code: ```powershell $action = New-ScheduledTaskAction -Execute 'C:\Scripts\MyScript.ps1' -Argument '-Param1 Value1 -Param2 Value2' $trigger = New-ScheduledTaskTrigger -At 9am -Daily $principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount Register-ScheduledTask -TaskName 'MyPowerShellTask' -Action $action -Trigger $trigger -Principal $principal ``` When I check the task history, I see an behavior message that states: "The task has not been run because the specified action is invalid." Additionally, when I try to run the task manually from the Task Scheduler, it executes but reports that the script is missing the parameters. I've tried wrapping the parameters in quotes and also using an array for the arguments, but nothing seems to work. I'm wondering if there's an scenario with how I'm formatting the parameters for the `New-ScheduledTaskAction` cmdlet, or if there's a specific compatible format I should be using for scripts. Any suggestions on how to resolve this would be greatly appreciated! I'm working on a application that needs to handle this. Has anyone else encountered this? How would you solve this? I recently upgraded to Powershell 3.11. Is there a simpler solution I'm overlooking?