Reputation: 11304
I’m experiencing an issue with a PowerShell script that creates and starts a scheduled task. This script works without any issues on Windows Server 2019. However, on Windows Server 2022, the task is created successfully and shows up with the status Ready
, but it does not start as scheduled until a user manually logs in to the server. Below code runs a powershell remoting session.
Here’s the PowerShell script I'm using:
$action = New-ScheduledTaskAction -Execute $ExeName -Argument $Arguments
$settings = New-ScheduledTaskSettingsSet
$adminGroup = (Get-LocalGroup -SID 'S-1-5-32-544').Name
$principal = New-ScheduledTaskPrincipal -GroupID $adminGroup -RunLevel Highest
$task = New-ScheduledTask -Action $action -Settings $settings -Principal $principal
Register-ScheduledTask -TaskName $TaskName -InputObject $task -Force | Out-Null
Start-ScheduledTask -TaskName $TaskName -AsJob | Out-Null
$ExeName
and $Arguments
are set to the correct executable and arguments.The task appears in Task Scheduler with the status Ready
, but it doesn’t trigger until a user logs in, which defeats the purpose of automation.
Administrators
since the executable requires it.Is there any difference in task scheduling behavior between Windows Server 2019 and 2022 when tasks are created and started from a PowerShell remoting session? Any help or suggestions would be greatly appreciated.
Upvotes: 0
Views: 360
Reputation: 190
Not sure how to adjust this in PowerShell, but for the scheduled task, you need to choose "Run whether user is logged on or not", which will ask for credentials.
Upvotes: 0