Selective Automation Script Schedules

We now have our PowerShell Universal instance in production with a staging environment (both in Azure App Services) and two guys working on their local machines. We started using scheduled scripts and are coming across the problem of how to make sure the schedules only trigger in production. The best we could come up with at the moment is doing

if($env:WEBSITE_SLOT_NAME -eq “Production”) { New-PSUSchedule -Cron “0 */4 * * *”… }

To the schedules.ps1 file. This has the desired effect, but if we edit the schedules in the UI, it completely removes the if statement.
My first thought was to have the if check in the script itself, but then we wouldn’t be able to test the script locally.

Any Ideas?

Product: PowerShell Universal
Version: 1.5.16

This sounds like a good feature request but here’s a workaround you might be able to do:

In the script, you should be able to add a force parameter and then for local development, you could force the script to run.

param([Switch]$Force)

if($env:WEBSITE_SLOT_NAME -eq “Production” -or $Force) { 
     #run script
 }

1 Like

Thanks for the suggestion, we went with yours for the time being.
Any ideas on how that feature might be implemented? I was thinking maybe on the New-PSUSchedule cmdlet, maybe parameters to pass would be the name of the variable to check and another one for it’s needed value? Might look like this.
New-PSUSchedule -Cron “0 */4 * * *” -Script “Script1.ps1” -TimeZone “…” -AllowedEnv “WEBSITE_SLOT_NAME” -AllowedValue ‘Production’

I don’t necessarily like the parameter names I have there, can’t think of anything that sounds good lol. Also could be better ways of handling it.

1 Like

I would suggest a more generic way by having a -ConditionScript parameter.

Example:

-ConditionScript {$env:WEBSITE_SLOT_NAME -eq “Production”}

If it is returning $true it will be executed.
For an advanced level it would be great to get some context variables like the Script information.

2 Likes

Oh I like that idea. +1 from me on that.