Home / Guides / Find failed scheduled tasks on many servers

Find Windows scheduled tasks that failed or did not run, on many servers

The nightly export, the cleanup script and the certificate renewal all live in Task Scheduler, and Task Scheduler does not tell anyone when one of them fails. You find out when a report is empty or a disk is full. This guide shows how to read the last result of every task with PowerShell, what the result codes mean, a read-only script that lists the problem tasks on any number of servers, and where that approach stops.

Short answer.

Get-ScheduledTask | Get-ScheduledTaskInfo returns LastRunTime, LastTaskResult, NextRunTime and NumberOfMissedRuns for each task. A LastTaskResult of 0 is a success; 0x41303 means the task has never run; anything else is either the exit code of the program the task started or a Task Scheduler error code. Add -CimSession to read another server.

Read one computer

The two cmdlets are in the ScheduledTasks module that ships with Windows 8 and Windows Server 2012 and later:

Get-ScheduledTask |
    Where-Object { $_.TaskPath -notlike '\Microsoft\*' -and $_.State -ne 'Disabled' } |
    Get-ScheduledTaskInfo |
    Select-Object TaskPath, TaskName, LastRunTime,
        @{ n = 'LastResult'; e = { '0x{0:X}' -f [int64]$_.LastTaskResult } },
        NextRunTime, NumberOfMissedRuns |
    Sort-Object LastResult -Descending |
    Format-Table -AutoSize

Nothing here changes a task; the cmdlets only read. Get-ScheduledTaskInfo gives the information from the task's last run, so a task that failed three days ago and has not run since still shows that failure.

What LastTaskResult means

The number is a mix of two things, and that is the part that trips people up. If the task started a program, the value is usually that program's exit code, so 0 is success and 1 or 5 is whatever the program meant by it. Some programs return a non-zero code on success (robocopy returns 1 when it copied files), so decide per task what is normal. Otherwise it is one of the Task Scheduler codes that Microsoft documents. The ones you will meet most:

ValueNameMeaning
0x0The task ran and the program returned 0.
0x41300SCHED_S_TASK_READYReady to run at its next scheduled time.
0x41301SCHED_S_TASK_RUNNINGRunning right now.
0x41303SCHED_S_TASK_HAS_NOT_RUNThe task has not run yet. In PowerShell it prints as 267011.
0x41304SCHED_S_TASK_NO_MORE_RUNSThere are no more runs scheduled for this task.
0x41306SCHED_S_TASK_TERMINATEDThe last run of the task was terminated by the user.
0x41325SCHED_S_TASK_QUEUEDTask Scheduler has asked the task to run.
0x8004131FSCHED_E_ALREADY_RUNNINGAn instance of this task is already running.
0x80041320SCHED_E_USER_NOT_LOGGED_ONThe task will not run because the user is not logged on.
0x80041324SCHED_E_TASK_ATTEMPTEDTask Scheduler tried to run it, but a constraint in the task definition stopped it.

Microsoft's page lists the rest, including every SCHED_E_ error. Convert a decimal value to hex with '0x{0:X}' -f 267011.

Read many servers with one script

This script reads each computer with a CIM session, skips disabled tasks and the \Microsoft\ folder (Windows' own housekeeping), and prints one row for every task that never ran, has nothing left to run, last returned a code you do not count as success, or has missed runs. A server it cannot reach gets one row saying why, so a silent server is never mistaken for a healthy one.

<#
 Read-only: lists enabled Task Scheduler tasks that failed, never ran, have a missed run or have nothing left to run,
 on one or many computers. Needs the ScheduledTasks module (Windows 8 / Server 2012 and later). For another computer
 the account must be a local administrator there and WinRM (the default CIM transport) must be enabled.
#>
param(
    [string[]] $ComputerName = @($env:COMPUTERNAME),
    [switch]   $IncludeMicrosoftTasks,                 # \Microsoft\* tasks are skipped by default (Windows' own housekeeping)
    [int[]]    $AlsoSuccess = @(),                     # extra exit codes you treat as success, e.g. 1 for a robocopy task
    [pscredential] $Credential                         # optional, for other computers
)

# Not failures: 0 = the program returned 0, 0x41300 ready, 0x41301 running, 0x41325 queued.
$ok = @(0, 0x41300, 0x41301, 0x41325) + $AlsoSuccess

foreach ($pc in $ComputerName) {
    $session = $null
    try {
        $cim = @{}
        if ($pc -notin @($env:COMPUTERNAME, 'localhost', '.')) {          # this computer needs no session
            $sessionArgs = @{ ComputerName = $pc; ErrorAction = 'Stop' }
            if ($Credential) { $sessionArgs.Credential = $Credential }
            $session = New-CimSession @sessionArgs
            $cim = @{ CimSession = $session }
        }
        $tasks = Get-ScheduledTask @cim -ErrorAction Stop |
                 Where-Object { $_.State -ne 'Disabled' -and ($IncludeMicrosoftTasks -or $_.TaskPath -notlike '\Microsoft\*') }
        foreach ($t in $tasks) {
            $i = $t | Get-ScheduledTaskInfo -ErrorAction SilentlyContinue
            if (-not $i) { continue }
            $r = [int64]$i.LastTaskResult
            $problem = $null
            if     ($r -eq 0x41303)                         { $problem = 'Never ran' }
            elseif ($r -eq 0x41304)                         { $problem = 'No more runs scheduled' }
            elseif ($ok -notcontains $r)                    { $problem = 'Last result not a success' }
            elseif ($i.NumberOfMissedRuns -gt 0)            { $problem = "Missed $($i.NumberOfMissedRuns) run(s)" }
            if ($problem) {
                [pscustomobject]@{
                    Computer = $pc; Task = ($t.TaskPath + $t.TaskName); Problem = $problem
                    LastResult = ('0x{0:X}' -f $r); LastRun = $i.LastRunTime; NextRun = $i.NextRunTime
                    RunAs = $t.Principal.UserId
                }
            }
        }
    }
    catch {
        [pscustomobject]@{ Computer = $pc; Task = ''; Problem = "Could not read: $($_.Exception.Message.Trim())"
                           LastResult = ''; LastRun = $null; NextRun = $null; RunAs = '' }
    }
    finally { if ($session) { Remove-CimSession $session } }
}

Run it like this:

# This computer
.\Get-TaskProblems.ps1

# Many servers, one list, saved as CSV
$servers = Get-Content .\servers.txt
.\Get-TaskProblems.ps1 -ComputerName $servers | Export-Csv .\task-problems.csv -NoTypeInformation

# A task that returns 1 on success is not a failure for you
.\Get-TaskProblems.ps1 -ComputerName $servers -AlsoSuccess 1

I ran it on a Windows 11 test machine against tasks I created: one that exits 0, one that exits 1, one that exits 5, one never run and one disabled. It listed the three that should be listed, left out the healthy and the disabled one, and with -AlsoSuccess 1 dropped the exit-code-1 task. An unreachable name came back as a "Could not read" row with the reason. I did not test another working server, so read the next section before you rely on it. Real machines add noise: tasks that start at logon or on an event also show 0x41303 until the trigger fires for the first time, so expect a few "never ran" rows to be fine.

What you need for other computers

  • WinRM. New-CimSession uses WS-Management (WinRM) by default. Where it is off, the session fails with a message about the WinRM service; Enable-PSRemoting on that server turns it on. Computers outside your domain also need HTTPS or a TrustedHosts entry.
  • Rights. Run as an account with enough rights on each server to read its tasks; in practice a member of the local Administrators group. A short list can be a permissions problem, not an empty server, so check a server you know has tasks.
  • Times. Task times are from each server's own clock. Compare them against that server's time zone.

Run it on a schedule and send the result

Run the script from a task on one management server (a scheduled task that checks scheduled tasks), pipe the rows into Send-MailMessage or your webhook of choice, and send nothing when the list is empty. Keep two limits in mind. First, if the machine that runs the check is down, the check is down: add something that tells you it did not run. Second, the script sees only the last run of each task. A task that fails every other night looks healthy on the nights it passes, and a task that runs every five minutes can fail and recover between two checks.

Where the script stops

  • "Did not run" is a guess. The script reports a task as never run or as having missed runs. It does not know that a daily task was due at 02:00 and it is now 02:30 with no new LastRunTime. To catch that you compare each task's last run with its expected schedule, which means a per-task rule you maintain.
  • Exit codes need a decision per task. Whether 1 is a failure is up to the program. The -AlsoSuccess list is global, so one task's normal code hides another task's failure.
  • No duration, no history. A task that has been running for six hours shows 0x41301, which the script treats as fine, and it keeps no history between runs.
  • SQL Server Agent jobs are somewhere else. They live in msdb, not in Task Scheduler, and need their own query; see Check SQL Server Agent job failures across many servers.

Questions

How do I see whether a scheduled task succeeded?

Run Get-ScheduledTask | Get-ScheduledTaskInfo and look at LastTaskResult. 0 means the program the task started returned 0. 0x41303 means the task has never run. Any other value is the program's exit code or a Task Scheduler code listed in Microsoft's error and success constants.

What does 0x41303 or 267011 mean in Task Scheduler?

It is SCHED_S_TASK_HAS_NOT_RUN: the task has not yet run. 267011 is the same number in decimal. A task with a logon or event trigger shows it until the trigger fires for the first time.

Does a result of 1 or 0x1 always mean the task failed?

No. For a task that starts a program, the result is normally that program's exit code, and what a non-zero code means is up to the program. Robocopy, for example, returns 1 when it copied files. Decide per task which codes are normal.

Can I check scheduled tasks on a remote server with PowerShell?

Yes. Create a session with New-CimSession -ComputerName and pass it to Get-ScheduledTask with -CimSession. It uses WinRM by default, so WinRM must be enabled on the remote server, and you need rights to read that server's tasks.

Is there a tool that watches Task Scheduler tasks on many servers?

TaskPier reads Task Scheduler tasks and SQL Server Agent jobs on the servers you list and alerts you by email, Teams, Slack or a webhook when one failed, was missed, ran too long or a server could not be read. It only reads and never starts or changes a task.

Related

Sources (Microsoft Learn): Get-ScheduledTask, Get-ScheduledTaskInfo, Task Scheduler error and success constants, New-CimSession.

An unhandled error has occurred. 🗙