Windows Task Scheduler allows you to run tasks both on schedule and when a certain event appears in the Event Viewer (described in detail in the post “Using Event Triggers in Windows“). In this article, we’ll show you how to create a scheduled task that automatically runs after another task completes successfully.
Suppose you want to run a Pong scheduler task after the Ping job completes successfully.
- Open the Task Scheduler console (
Taskschd.msc) and find the Ping task;
- Click the History tab on the bottom taskbar. It contains the complete history of events related to this scheduled task; If Task Scheduler only shows the History (Disabled) tab, you need to click Enable All Task History in the right Actions pane. After that, all task events will be displayed in the History tab.
- We need the event with the Event ID 102 (
Task completed) which appears after the successful completion of the task (
Task Scheduler successfully finished);
- Open the properties of this event, go to the Details tab, and switch to the XML View of the event. We will use the following data from XML when building the condition statement for the new scheduled task:
EventID: 102 Provider-Name: Microsoft-Windows-TaskScheduler Channel: Microsoft-Windows-TaskScheduler/Operational TaskName: \MyTasks\Ping
eventvwr.msc) and go to the Applications and Services Log -> Microsoft -> Windows -> Task Scheduler -> Operational. Right-click on the item and select the Enable Log option.
Now you can create a Pong scheduled task.
When you create a trigger for a Pong job, you must specify the condition for triggering the job when event 102 appears (New Trigger -> On an event). But the problem is that EventID 102 appears after any task is completed, not only the Ping task.
You can create more flexible conditions for selecting events (Custom) when the standard filter does not help you select an event accurately enough. Click on the New Event Filter button.
Create a new filter by specifying the previously retrieved data from the XML View of the event.
- Events Logs: Microsoft-Windows-TaskScheduler/Operational
- Event source: TaskScheduler
- Task category: Task completed
Click the XML tab. It will show the XML representation of your filter (XPath):
<QueryList> <Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational"> <Select Path="Microsoft-Windows-TaskScheduler/Operational">*[System[Provider[@Name='Microsoft-Windows-TaskScheduler'] and Task = 102]]</Select> </Query> </QueryList>
Check the Edit query manually option. You need to bind your filter to the \MyTasks\Ping task. To do this, replace the following line in the XML filter:
*[System[Provider[@Name='Microsoft-Windows-TaskScheduler'] and Task = 102]]
Save your filter settings in the Triggers (Custom Event Filter) tab and save the Pong task.
Now try to run the Ping task (manually, scheduled, or using PowerShell:
Start-ScheduledTask mytasks\ping). When the Ping task completes successfully, the Pong job will be started immediately.
The XPath format is shown below.
In this way, you can set up entire task chains, to run the scheduler tasks in sequence. Similarly, you can create any other dependencies in Windows Scheduler tasks. For example, if the backup job is completed successfully, you run one script, and if the backup fails, you need to run a script to clean up or fix the current state.