Monday, March 4, 2019

Trigger Windows Scheduled Task to Run Upon the Successful Completion of Another Scheduled Task

Images by PublicDomainPictures

In one of the projects that I worked on, I have two tasks/jobs on Windows Task Scheduler, let say the name of the first job is “FirstJob” and the second job is “SecondJob”. I have a requirement where the “SecondJob” should run immediately after the “FirstJob” has been completed successfully. There are several ways to achieve this. For this particular project, to fulfil the requirement, on the “SecondJob”, I would trigger the “SecondJob” based “On an event”, which the the successful completion of the “FirstJob”.

The steps that I’ve taken to create the trigger on the “SecondJob”:

  • On the “SecondJob” property, within the Task Scheduler, go to “Triggers” tab. And click on the “New” button.
  • On the “New Trigger” window, for “Begin the task”, choose “On an event.” Then on the “Settings” section, click on the “Custom” radio button. And click on the “New Event Filter…” button.
  • On the “New Event Filter” window, click on the “XML” tab, and check the “Edit query manually” checkbox.
  • You might get the following message: “If you choose to manually edit the query, you will no longer be able to modify the query using the controls on the Filter tab. Would you like to continue?” Click the “Yes” button to continue.
  • At this point you just need to enter the XPath of the event filter. This will be used to query the Windows Event. In my case, this is where I want to specify the trigger to start “SecondJob” when the “FirstJob” has been completed successfully. The XPath event filter that I was using:

The XPath event filter code:

<Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational">
<Select Path="Microsoft-Windows-TaskScheduler/Operational">*[EventData[@Name='ActionSuccess'][Data [@Name='TaskName']='\FirstJob']] and *[EventData[@Name='ActionSuccess'][Data [@Name='ResultCode']='0']]</Select>

You can adjust for the above code to your situation. The most likely would be the Task Name.

Notes: In Windows 10, by default the Task History for Task Scheduler is disabled. In order to make this work, you will need to enable it. To do this you can go to the Task Scheduler, and without selecting any task, click on the “Action” on the top menu and then “Enable All Tasks History”.

No comments:

Post a Comment