Isolating Sitecore Scheduled Tasks to Specific Environments

> Useful for database refreshes
Cover Image for Isolating Sitecore Scheduled Tasks to Specific Environments

The Problem

As a complement to my post on database refreshes, I've written a PowerShell script chunk that can be used to isolate Sitecore scheduled tasks to specific environments (assuming that your scheduled task is performed using Sitecore PowerShell Extensions).

This is useful for preventing production-only scheduled tasks from running in downstream environments after a refresh. It saves you the step of having to make manual changes.

The Script

$environmentName = [System.Configuration.ConfigurationManager]::AppSettings["env:define"]
$serverRole = [System.Configuration.ConfigurationManager]::AppSettings["role:define"]
Write-Host "Environment: $environment"
Write-Host "Server Role: $serverRole"
$productionEnvironmentName = "Production"
# Note about the '.Contains()'. You must use that syntax because '-contains' doesn't work
if(-not $environmentName -eq $productionEnvironmentName
-or -not $serverRole.Contains("ContentManagement")){
Write-Host "This should only run on PROD CM. Exiting."
exit;
}
# Your other code...

Future Enhancements

In discussions with Michael West, he suggested that this can also be accomplished using the rules engine in Sitecore. There aren't any OOB rules that check the Sitecore configuration, but it would be ideal to compare those values before executing any scripts. That way, we don't need to add the code snippet above to every script.

Stay in your lane,

-MG

I chose these words

More Posts

Cover Image for Tips for Forms Implementations

Tips for Forms Implementations

> And other pro tips

Cover Image for Learnings from 48 hours of battling AI-augmented hackers

Learnings from 48 hours of battling AI-augmented hackers

> Hackers are coming after Sitecore sites too

Cover Image for Tips for New Sitecore Developers

Tips for New Sitecore Developers

> If I had more time, I would have written a shorter letter

Cover Image for Stale Referenced Content in SitecoreAI

Stale Referenced Content in SitecoreAI

> One Experience Edge setting that I wish was on by default