Skip to main content

Sitecore

Create a Scheduled Task to Run PowerShell Script with Sitecore Task Scheduler

Istock 691171106 (2)

We learned about how to create a Sitecore task scheduler in the previous article Create a scheduled task in Sitecore.

In this article, we will learn about to Sitecore task scheduler using PowerShell Script

For Example, To unpublish some Sitecore items after a certain date at a certain time. Consider, we have a PowerShell script to unpublish a few specific items based on any business logic. We want this PowerShell script to run at specific intervals of time. In this scenario, the Sitecore scheduler will be a good option for running those PowerShell scripts.

Step 1:  Create PowerShell Script

Assume that we have requirements like if the event session date passed, then this event should not show on the event list, so we will unpublish that particular item. For this, we can write a PowerShell script as below.

We used the Event session template in the PowerShell script.

Event Template

Go to Sitecore Launchpad and open PowerShell ISE or go to the Desktop and open Sitecore -> Development Tools -> Sitecore ISE. 

It launches an Integrated Scripting Environment. Write your PowerShell scripts and test them to see if they work properly.

$node ="/sitecore/content/Demo/Perficient/Home/Events"
    $items = Get-Item -Path "master:" -Query $node | Get-ChildItem -rec  
    $Today =[int] (Get-Date). ToString("yyyyMMdd")
    foreach ( $child in $items ) {
       if(($child.TemplateName -eq "Event Session") -and ($child["Session End Date"]) -and ([int]$child["Session End Date"].Split("T")[0] -lt $Today) -and($child["__Never publish"] -eq "")) 
       {       
            $child.Editing.BeginEdit() 
            $child["__Never publish"] = "1"
            $child.Editing.EndEdit() 
        }
    }   
$database = Get-Database -Name "master"
$item = Get-Item -Path "/sitecore/content/Demo/Perficient/Home/Events" #Events folder
$targets = @()
foreach($publishingTarget in [Sitecore.Publishing.PublishManager]::GetPublishingTargets($database)) {
    $targets += Get-Database -Name $publishingTarget[[Sitecore.FieldIDs]::PublishingTargetDatabase]
}
$languages = [Sitecore.Data.Managers.LanguageManager]::GetLanguages($database)
# Item publish with children
[Sitecore.Publishing.PublishManager]::PublishItem($item,$targets,$languages,$true,$true,$true)

You can update your PowerShell script as per your requirement.

Step 2: Save the Script

I save the script named “Unpublish Event Session ” and locate it in the content tree under the “/sitecore/system/Modules/PowerShell/Script Library” path.Unpublish Event Session

Step 3: Create a Scheduler

Go to the Scheduler path by /sitecore/system/Tasks/Schedules. Right Click on Schedules and select the PowerShell Scripted Task Scheduler option from Insert Menu.  You can also select Schedule, but here I am selecting the PowerShell Scripted Task Scheduler option.

Powershellscheduleoption

It opens the “create new scripted schedule” window; fill in the Name for the task schedule as I have named it UnpublishEventScheduler, Select Location, Select Script (that we created earlier steps), and click on Create.

Unpublisheventscheduler

Opened Schedule for task: UnpublishEventScheduler popup window; fill in all the below information as per your requirement and click on change.

UnpublishschedulerNow your scheduler is created as UnpublishEventScheduler under the”/sitecore/system/Tasks/Schedules/UnpublishEventScheduler” path.

Event Scheduler

 

If you want to select the Schedule option for the scheduler, see the blog post: Create a scheduled task in Sitecore.

If the scheduler needs to be updated or modified, then right-click on your Schedule item (UnpublishEventScheduler), then, go to the Scripts and Select “Edit Task Schedule” to open a dialog to edit the scheduled task.

and If the task needs to be run manually, choose “Run Task Schedule.”

Edit Task

Now your scheduler is ready to schedule.

I hope it’s useful. Happy Learning!

 

Thoughts on “Create a Scheduled Task to Run PowerShell Script with Sitecore Task Scheduler”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Raksha Janbandhu

Raksha Janbandhu is a Technical Consultant at Perficient. She has over 6 years of experience in Development and has contributed to Sitecore projects based on SXA, Score, ASP.NET MVC, and Web Forms.

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram