PowerShell 7.3 - implementing Setting Up Scheduled Tasks with User Context and Run As Parameter
I'm testing a new approach and I'm having trouble creating a scheduled task in PowerShell 7.3 that runs under a specific user context... I've tried using the `New-ScheduledTask` and `Register-ScheduledTask` cmdlets, but the task seems to unexpected result when running under the specified user account, even though I've verified the credentials are correct. Hereβs the code I attempted: ```powershell $action = New-ScheduledTaskAction -Execute 'C:\Path\To\YourScript.ps1' $trigger = New-ScheduledTaskTrigger -At 2am -Daily $principal = New-ScheduledTaskPrincipal -UserId 'DOMAIN\username' -LogonType Password -RunLevel Highest Register-ScheduledTask -TaskName 'MyTask' -Action $action -Trigger $trigger -Principal $principal ``` When testing the task, I receive the following behavior: ``` Task Scheduler behavior: 0x80070569 - Access is denied. ``` I've checked the user account permissions, and it should have the required rights to run the script. I've also ensured that the task is set to run with the highest privileges, but it still fails. Additionally, Iβve confirmed that the user can manually run the script without any issues. Could there be something I'm missing regarding the user context or session that might be causing this? Any insights on best practices for setting up scheduled tasks in PowerShell would be appreciated. I'm on Debian using the latest version of Powershell. Has anyone dealt with something similar? I recently upgraded to Powershell 3.11. Is this even possible?