Wednesday, August 14, 2019

Running a Powershell Script by scheduled Task

Sitecore version:8.2.

You can find lot of articles online,for my reference posting this article.
Running powershell script for scheduled task is not that complex,you can create a powershell script and run schedule basis.the below script is for clearing the cache on a schedule basis, best practice is not to clear the cache often,it will effect to your site performance.

Basic script:
ForEach($site in [Sitecore.Configuration.Factory]::GetSiteNames()) {
    $siteInfo = [Sitecore.Configuration.Factory]::GetSiteInfo($site)
    if($siteInfo -ne $null) {
         $siteInfo.HtmlCache.Clear()  
         $logEntry = [string]::Format("mahe through Scheduled Task your HtmlCache.Clear() invoked for {0}", $siteInfo.Name)
         Write-Log $logEntry
         Write-Host $logEntry
    }
}
1)      Write above script in your powershell ISE and excute if it’s working fine save as [some name] PowerShellHtmlCacheClearTask
2)      /sitecore/system/Modules/PowerShell/Script Library/Task Management/Content Editor/Insert Item/PowerShellHtmlCacheClearTask








 Creating the Scheduled Task
  1.  Goto /sitecore/system/Tasks/Schedules
  2.  Click on the Schedules and insert PowerShell Scripted Task Schedule




1     Give the name SchedulePowerShellScript
2     Select the Powershell task you created in 1st step
       Click on the create



Select the timings when you want to run and how often it should run



You will see on the SPElogs your task running



No comments:

Post a Comment