PowerShell 7.3 - Difficulty in Creating a Scheduled Task that Runs with Elevated Privileges without User Interaction
I've searched everywhere and can't find a clear answer... I'm sure I'm missing something obvious here, but Hey everyone, I'm running into an issue that's driving me crazy. I'm trying to create a scheduled task in PowerShell 7.3 that should run a script with elevated privileges, but I'm running into issues with it not executing as expected without user interaction. My current approach is to use the `New-ScheduledTask` and `Register-ScheduledTask` cmdlets. Hereβs what I've tried so far: ```powershell $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\MyScript.ps1"' $trigger = New-ScheduledTaskTrigger -AtStartup $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount $task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal Register-ScheduledTask -TaskName "MyElevatedTask" -InputObject $task ``` However, when I check the task in Task Scheduler, it shows that the task is ready, but when the system starts, it doesn't seem to run. I checked the history, and I see the behavior: `The task is currently running, but the request for the user is not valid.` I suspect this is due to the `LogonType` setting not being compatible with the script's requirements. I've also tried changing the `LogonType` to `Interactive` and `InteractiveToken`, but those options require a logged-on user, which contradicts my goal of having it run unattended. Is there a way to ensure that my scheduled task runs with the necessary privileges without requiring a user to log in? Any insights on configuring it correctly or troubleshooting this scenario would be greatly appreciated! For context: I'm using Powershell on Windows. I'd really appreciate any guidance on this. This is happening in both development and production on CentOS. I'd be grateful for any help.