CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Scheduled Task Trigger Not Firing as Expected with Custom Script

๐Ÿ‘€ Views: 83 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-10
powershell scheduled-tasks windows

I'm reviewing some code and I'm having trouble with a scheduled task in PowerShell 7.3 that I've set up to run a custom script at a specific time... I configured the task using the `New-ScheduledTask` cmdlet, but it doesn't seem to trigger the script at the scheduled time. I verified the task is created using `Get-ScheduledTask`, and it shows the correct settings. However, when the time comes, I don't see any output or logs from the script. Hereโ€™s the code I've used to create the scheduled task: ```powershell $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-File "C:\Scripts\MyScript.ps1"' $trigger = New-ScheduledTaskTrigger -At '11:00AM' -Daily $principal = New-ScheduledTaskPrincipal -UserId 'NT AUTHORITY\SYSTEM' -LogonType ServiceAccount Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName 'MyCustomTask' ``` I checked the Task Scheduler, and it shows the task is enabled. I also tried running the script manually, and it works perfectly fine. Furthermore, I have ensured that the script does not require any user interaction. I suspect there might be an scenario with permissions or the context in which the script runs. Iโ€™ve tried changing the user for the scheduled task to my own account, but that didnโ€™t seem to help either. The Windows Event Viewer does not show any errors related to this task either. Can someone shed light on what could be going wrong? Is there a way to debug if the task is firing or check its exit code? Any help would be greatly appreciated! Any ideas what could be causing this?