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


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


More Stories

Cover Image for NextJS/JSS Edit Frames Before JSS v21.1.0

NextJS/JSS Edit Frames Before JSS v21.1.0

> It is possible. We have the technology.

Cover Image for Script: Boost SIF Certificate Expiry Days

Script: Boost SIF Certificate Expiry Days

> One simple script that definitely won't delete your system32 folder

Cover Image for Troubleshooting 502 Responses in Azure App Services

Troubleshooting 502 Responses in Azure App Services

> App Services don't support all libraries

Cover Image for Integrating Cloudflare Turnstile with Sitecore Forms

Integrating Cloudflare Turnstile with Sitecore Forms

> Smack down spam quickly and easily