Moerwald
Moerwald

Reputation: 11304

Scheduled Task Not Starting on Windows Server 2022 Until User Logs In

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

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.

What I've Tried:

Additional Information:

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

Answers (1)

Lord Helmet
Lord Helmet

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.

enter image description here

Upvotes: 0

Related Questions