Santa

Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

How to create a scheduled task to execute a PowerShell Script Using Sitecore Task Scheduler?

October 02, 2023
How to create a scheduled task to execute a PowerShell Script Using Sitecore Task Scheduler?
Keyur Garala
Keyur Garala
Sitecore Certified Solutions Architect
how-to-create-a-scheduled-task-to-execute-a-powerShell-script-using-sitecore-task-scheduler

In this guide, we will explore how to utilize the Sitecore task scheduler along with a PowerShell script. Specifically, we will focus on an example where we need to unpublish certain Sitecore items based on predetermined dates and times. By configuring the Sitecore scheduler, we can ensure that our PowerShell script executes at specific intervals, enabling us to effectively accomplish the task at hand.

Step 1: Creating the PowerShell Script

Let's assume we have a requirement where we need to remove events from the event list once their session date has passed. To achieve this, we can create a PowerShell script with the following logic.

In the PowerShell script, we will utilize the Event session template.

how-to-create-a-scheduled-task-to-execute-a-powerShell-script-using-sitecore-task-scheduler-1

Navigate to the Sitecore Launchpad and locate PowerShell ISE, or alternatively, go to the Desktop and access Sitecore -> Development Tools -> Sitecore ISE. This will open the Integrated Scripting Environment (ISE) specifically designed for PowerShell scripting within Sitecore.

Within the Sitecore ISE, you can proceed to write your PowerShell scripts and thoroughly test their functionality to ensure they perform as intended.

how-to-create-a-scheduled-task-to-execute-a-powerShell-script-using-sitecore-task-scheduler-2

Feel free to modify the PowerShell script according to your specific requirements. Customizing the script will allow you to tailor its functionality to meet your desired outcome.

Step 2: Saving the Script

Save the PowerShell script with the name "Unpublish Event Session" and store it within the content tree. Place it under the path "/sitecore/system/Modules/PowerShell/Script Library" for easy accessibility and organization.

how-to-create-a-scheduled-task-to-execute-a-powerShell-script-using-sitecore-task-scheduler-3

Step 3: Creating a Scheduler

To begin creating a scheduler, navigate to the Scheduler path within Sitecore by accessing "/sitecore/system/Tasks/Schedules". Right-click on the "Schedules" folder and select the "PowerShell Scripted Task Scheduler" option from the insert menu. Alternatively, you can choose the "Schedule" option, but for this scenario, we will specifically select the "PowerShell Scripted Task Scheduler" to configure our PowerShell script execution.

how-to-create-a-scheduled-task-to-execute-a-powerShell-script-using-sitecore-task-scheduler-4

This action will open the "Create New Scripted Schedule" window. Proceed to provide a suitable name for the task schedule, such as "UnpublishEventScheduler." Select the appropriate location for the schedule. Choose the previously created script from the list of available scripts. Finally, click on the "Create" button to finalize the creation of the scheduled task.

how-to-create-a-scheduled-task-to-execute-a-powerShell-script-using-sitecore-task-scheduler-5

In the "Schedule for task: UnpublishEventScheduler" popup window, enter the required information based on your specific needs. Customize the fields as necessary, and once you have entered all the relevant details, click on the "Change" button to save the changes.

how-to-create-a-scheduled-task-to-execute-a-powerShell-script-using-sitecore-task-scheduler-6

A scheduler named “UnpublishEventScheduler” is created and can be found at "/sitecore/system/Tasks/Schedules/UnpublishEventScheduler".

If you need to update or modify the scheduler, you can right-click on the Schedule item (UnpublishEventScheduler) and navigate to the Scripts section. From there, select "Edit Task Schedule" to open a dialog where you can make the desired changes to the scheduled task.

In case you want to manually execute the task, simply choose "Run Task Schedule" to initiate the execution of the scheduled task without waiting for the predefined schedule.


YOU MAY ALSO LIKE