Last Result Successful Monitor - PSJob Error Count

Custom.Windows.ScheduledTaskV2.PSScheduledJob.LastResult_Error.Monitor (UnitMonitor)

Knowledge Base article:

Summary

PowerShell Scheduled Job Last Result Monitor.

The monitor checks the last result of a PowerShell Scheduled Job for error items (exceptions).

Any error observed is considered a warning condition for scheduled job with trigger(s).

Configuration

Use overrides to:

Causes

The PowerShell script called by the Scheduled Job threw exceptions.

Resolutions

Log onto the affected computer as the user that created the PowerShell Scheduled Job and run the following PowerShell command to get access to the job's last result:

This allows further investigating why the script did not complete sucessfully. Refer to the "Additional" section in case changes to the job are required.

and/or Re-run Scheduled Job

Disable or delete the PowerShell Scheduled Job

Additional

TechNet documentation regarding PSScheduledJob:

- Windows PowerShell Scheduled Job Cmdlets

Element properties:

TargetCustom.TaskScheduler2.Task
Parent MonitorSystem.Health.AvailabilityState
CategoryAvailabilityHealth
EnabledTrue
Alert GenerateTrue
Alert SeverityMatchMonitorHealth
Alert PriorityNormal
Alert Auto ResolveTrue
Monitor TypeCustom.Windows.ScheduledTaskV2.PSScheduledJob.LastResult_Error.MonitorType
RemotableFalse
AccessibilityPublic
Alert Message
PS Scheduled Job failed to complete successfully
Last execution of PowerShell Scheduled Job '{1}' returned {4} errors. It did not run successfully.

Task Scheduler Computer: {3}
Task Path: {0}
Job Name: {1}
Job User: {2}

Error Count: {4}
Error Summary: {5}
RunAsDefault

Source Code:

<UnitMonitor ID="Custom.Windows.ScheduledTaskV2.PSScheduledJob.LastResult_Error.Monitor" Accessibility="Public" Enabled="true" Target="Custom.TaskScheduler2.Task" ParentMonitorID="Health!System.Health.AvailabilityState" Remotable="false" Priority="Normal" TypeID="Custom.Windows.ScheduledTaskV2.PSScheduledJob.LastResult_Error.MonitorType" ConfirmDelivery="false">
<Category>AvailabilityHealth</Category>
<AlertSettings AlertMessage="Custom.Windows.ScheduledTaskV2.PSScheduledJob.LastResult_Error.Monitor.AlertMessage">
<AlertOnState>Warning</AlertOnState>
<AutoResolve>true</AutoResolve>
<AlertPriority>Normal</AlertPriority>
<AlertSeverity>MatchMonitorHealth</AlertSeverity>
<AlertParameters>
<AlertParameter1>$Data/Context/Property[@Name='Path']$</AlertParameter1>
<AlertParameter2>$Data/Context/Property[@Name='Name']$</AlertParameter2>
<AlertParameter3>$Data/Context/Property[@Name='User']$</AlertParameter3>
<AlertParameter4>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</AlertParameter4>
<AlertParameter5>$Data/Context/Property[@Name="PSJobErrorCount"]$</AlertParameter5>
<AlertParameter6>$Data/Context/Property[@Name="PSJobErrorContent"]$</AlertParameter6>
</AlertParameters>
</AlertSettings>
<OperationalStates>
<OperationalState ID="NoError" MonitorTypeStateID="NoError" HealthState="Success"/>
<OperationalState ID="Error" MonitorTypeStateID="Error" HealthState="Warning"/>
</OperationalStates>
<Configuration>
<ComputerName>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</ComputerName>
<TaskPath>$Target/Property[Type="SchTaskLib!Custom.Windows.TaskSchedulerTask"]/TaskPath$</TaskPath>
<!-- by default do not tolerate any errors -->
<maxErrorCount>0</maxErrorCount>
<WindowsTasks>$Target/Property[Type="Custom.TaskScheduler2.Task"]/WinTasksDiscovered$</WindowsTasks>
<Debug>false</Debug>
<TimeoutSeconds>300</TimeoutSeconds>
</Configuration>
</UnitMonitor>