Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Windows Server 2019 / How to Run a Scheduled Task After Another Task Completes

June 8, 2023 PowerShellWindows 10Windows Server 2019

How to Run a Scheduled Task After Another Task Completes

Windows Task Scheduler allows you to run tasks both on schedule and when a certain event appears in the Event Viewer (using Windows Event Triggers). In this article, we’ll show you how to create a scheduled task that automatically runs after another task completes successfully.

In my example, I couldn’t merge two tasks into one because I needed to run a batch script as a different user after successfully completing the first script (task).

Suppose you want to run a Pong scheduler task after the Ping job completes successfully.

  1. Open the Task Scheduler console (Taskschd.msc) and find the Ping task;
  2. Click the History tab on the bottom taskbar. It contains the complete history of events related to this scheduled task; sheduler task completed event id 102
    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.
  3. We need the event with the Event ID 102 (Task completed) which appears after the successful completion of the task (Task Scheduler successfully finished);
  4. 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

event properties xml view

Make sure Operational logs are enabled for Task Scheduler events. Open the Event Viewer console (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. enable task scheduler event logs

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.

task sheduler: create event trigger

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

new task event filter by task scheduler log

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]]

with:

*[EventData [@Name='TaskSuccessEvent'][Data[@Name='TaskName']='\MyTasks\Ping']]

edit event filter: add dependency

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.

run scheduled task after another

The XPath format is shown below.

XPath explanation

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.

In one of the previous posts, we looked at another script that allows you to run a program/script if another program has been started or closed in Windows.

19 comments
6
Facebook Twitter Google + Pinterest
previous post
Using Attribute Editor in Active Directory Users and Computers
next post
How to Run Multiple Commands in One Line in PowerShell and CMD

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

19 comments

Dieter June 9, 2017 - 4:28 pm

Don’t work for me on Windows 10 x64

Reply
Jon August 7, 2022 - 5:02 am

Do you have “All Tasks History” enabled for Task scheduler?
Also do you have Operational Event Lots Enabled?

This Superuser post has some screenshots superuser.com/a/1735734/140631

Reply
Terry Odom November 7, 2018 - 6:40 pm

For the section “Change the XPath code” it looks like you pasted the wrong code for us to use.

Reply
toto February 7, 2019 - 11:35 am

XPath code example in plain text is wrong when correct code is showed in screenshot.
Of course XML code should reference the actual task name from which completion notification should be triggered:

*[EventData[@Name=’TaskSuccessEvent’][Data[@Name=’TaskName’]=’\ping’]]

Reply
admin February 8, 2019 - 5:12 pm

You are right, the code is incorrect. This is an old wordpress problem with cutting out some special characters.
I still haven’t fixed it. Sorry.

Reply
Sigalit April 10, 2019 - 4:40 pm

This xPath code uses wrong kind of quotes. It didn’t work for me. So I used ‘ instead of ’ such in:

*[EventData[@Name=’TaskSuccessEvent’][Data[@Name=’TaskName’]=’\DBPopulations\test1′]]

Reply
Sigalit April 10, 2019 - 4:43 pm

Sorry but it seems that when copying the code here, the quotes change to the wrong kind.

Reply
Chris February 11, 2019 - 7:31 pm

Worked for me – this is my event trigger:

*[EventData[@Name=’ActionStart’][Data[@Name=’TaskName’]=’\Development\Start NI Error Reporting Service’]]

Reply
RIS July 22, 2019 - 10:01 am

Hello!
Thanks for a good article!

I have one question:
Is there a way to prevent running next task when previous was cancelled by user ?

Reply
RIS July 22, 2019 - 10:01 am

I tried this way, but to no avail (
*[EventData[@Name=’StoppedOnRequest’][Data[@Name=’TaskName’]=’\taskname’]]

Reply
RIS July 22, 2019 - 10:03 am

_tag-start_Suppress Path=”Microsoft-Windows-TaskScheduler/Operational”_tag_end_*[EventData[@Name=’StoppedOnRequest’][Data[@Name=’TaskName’]=’\Trol\PostgreSQL 1C bases backup\ACC_C_30_GK’]]_tag_start_/Suppress_tag_end_

Reply
RIS July 22, 2019 - 10:02 am

_tag-start_Suppress Path=”Microsoft-Windows-TaskScheduler/Operational”_tag_end_*[EventData[@Name=’StoppedOnRequest’][Data[@Name=’TaskName’]=’\Trol\PostgreSQL 1C bases backup\ACC_C_30_GK’]]_tag_start_/Suppress_tag_end_

Reply
Mark Davis January 2, 2020 - 12:03 am

This is awesome! Thank you so much…works like a charm!!

Reply
Bouwer February 11, 2020 - 1:46 pm

How would I change the above code to look for multiple successes?
Code below is not working.

*[EventData
[@Name=’TaskSuccessEvent’]
[Data[@Name=’TaskName’]=’\SQL\LoanTran\LoanTran (191 – 195)’]]

and

*[EventData
[@Name=’TaskSuccessEvent’]
[Data[@Name=’TaskName’]=’\SQL\LoanTran\LoanTran (196 – 200)’]]

Reply
pali April 29, 2021 - 9:05 am

Use “or” instead of “and”.

Reply
mahdi October 26, 2020 - 7:21 am

Tanks
It work for me nice and easy.

Reply
Azad Khan October 8, 2021 - 4:55 pm

This works for me on first success – but when target task runs again next day it doesn’t trigger

Reply
Jon August 6, 2022 - 11:57 am

does it work with a separate, test task? or on a different machine or VM?

Reply
Jon August 6, 2022 - 7:37 am

Absolute lifesaver, thank you OP. Using Win2012 R2 server and mirrored on Win10 LTSC. I reached the 32 action limit for my task, and needed a reliable way to spread out the actions task so that I can have all the actions done in sequence. Time-based trigger wouldn’t be accurate, especially if you randomize [delay] task #1 trigger time. Task #2 and subsequent task can be daisy chained to And the author somehow accidentally copied the old custom xml for the new custom xml. The steps are correct; follow the screenshots carefully.

Also, others are correct that the comments here will change the type of apostrophe ‘ to a more slanted ‘.
*[EventData[@Name=’TaskSuccessEvent’][Data[@Name=’TaskName’]=’\!TriggerByEvent Test’]]

So if you are copying from comments (or anywhere really) you should copy it into a plain text editor and check/replace out all the ‘ first. Make sure the overall formatting matches the new custom xml screenshot.

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Delete Old User Profiles in Windows
  • Get-ADUser: Find Active Directory User Info with PowerShell
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top