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 Use Ansible to Manage Windows Machines

September 25, 2023

Installing Language Pack in Windows 10/11 with PowerShell

September 15, 2023

Configure Email Forwarding for Mailbox on Exchange Server/Microsoft...

September 14, 2023

How to View and Change BIOS (UEFI) Settings...

September 13, 2023

How to Create UEFI Bootable USB Drive to...

September 11, 2023

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 Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023
  • How to Create UEFI Bootable USB Drive to Install Windows

    September 11, 2023
  • Redirect HTTP to HTTPS in IIS (Windows Server)

    September 7, 2023
  • Add an Additional Domain Controller to an Existing AD Domain

    September 6, 2023
  • How to Install an SSL Certificate on IIS (Windows Server)

    September 5, 2023
  • Managing Windows Firewall Rules with PowerShell

    August 31, 2023
  • Fixing ‘The Network Path Was Not Found’ 0x80070035 Error Code on Windows

    August 30, 2023

Follow us

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Configuring Port Forwarding in Windows
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • 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