Schedule PowerShell Scripts with PowerShell Universal

Image Description

Daily PowerShell #11

Scripting PowerShell Universal Daily PowerShell

October 28, 2021

quote Discuss this Article

PowerShell Universal is a platform for building web-based tools with PowerShell. One of the main features of PowerShell Universal is the ability to run PowerShell scripts on a schedule. In this post, we’ll look at the various ways to configure PSU schedules.

Create a Schedule

To create a basic schedule, you first need a script to schedule. Create a script by clicking Automation \ Scripts and then Create New Script.

PowerShell Universal Script

PowerShell Universal Script

Once the script has been created, you can click Automation \ Schedules and click Create new Schedule.

Simple Schedule

To create a simple schedule, select the script you wish to schedule and select the time you would like to schedule. Some basic options include Every Minute, Every Hour and Every Day at Midnight.

PowerShell Universal Create Simple Schedule

PowerShell Universal Create Simple Schedule

Once scheduled, you will be able to see the next execution time of the schedule.

PowerShell Universal Schedules

PowerShell Universal Schedules

Continuous Schedule

A continuous schedule run over and over again with a delay in between each execution. This is useful for scripts you wish to run as soon as possible but may take a variable amount of time between each execution.

To create a continuous schedule, click Create New Schedule and then go to the Continuous tab. Input the number of seconds to delay in between each execution.

PowerShell Universal Create Continuous Schedule

PowerShell Universal Create Continuous Schedule

Once scheduled, the execution time will be listed as soon as possible.

CRON Schedule

CRON expressions are helpful for defining custom, complex scheduling.

To create a CRON schedule, click Create new Schedule and then go to the CRON tab. Enter the CRON expression. Once created, the next execution time will be listed in the schedule table.

PowerShell Universal CRON Schedule

PowerShell Universal CRON Schedule

One-Time Schedule

A one-time schedule will run once in the future. To create a one-time schedule, click Create New Schedule and click the One-Time tab. Select the time in the future you would like it to run.

PowerShell Universal One-Time Schedule

PowerShell Universal One-Time Schedule

Script Parameters

Scripts that define a param block can accept parameters based on the schedule. To accept parameters in your script, adjust the contents to include a param block.

param($MyParameter)

$MyParameter

Now when you schedule a script, you will have the option to define parameters for the script.

PowerShell Universal Schedule Parameters

PowerShell Universal Schedule Parameters

Environments

Environments define which version or configuration of PowerShell to execute the scheduled script in. By default, PowerShell Universal runs scripts in the Integrated environment. The integrated Environment runs within the PowerShell Universal server. If you wish to run in an alternate version of PowerShell, select the environment when defining your schedule.

PowerShell Universal Schedule Environment

PowerShell Universal Schedule Environment

Run A Schedule as Another User

To run a schedule as another user, you first need to define the user’s credentials. You can do so by creating a secret variable. Click Platform \ Variables. Then click Create New Variable and select the Secret tab.

PowerShell Universal Secret Variable

PowerShell Universal Secret Variable

Once you have a credential variable created, you can create a schedule and specify the account to run the schedule as.

PowerShell Universal Schedule Credential

PowerShell Universal Schedule Credential

Configuring Schedules in PowerShell

You can use the schedules.ps1 file to configure schedules via PowerShell script. To edit or view the configuration file, navigate to Settings \ Configurations and click schedules.ps1.

A schedules that are defined in the UI will show within this file. You can also edit the file manually to add new schedules.

New-PSUSchedule -Cron "* * * * *" -Script "Script.ps1" -TimeZone "America/Denver" -Credential "MyUser" 
New-PSUSchedule -Script "Script.ps1" -TimeZone "America/Denver" -Continuous -Delay ([System.TimeSpan]::FromSeconds(10)) -Credential "Default" 
New-PSUSchedule -Cron "0 */8 * * *" -Script "Script.ps1" -TimeZone "America/Denver" -Credential "Default"

View Schedule Results

You can view the resulting jobs based on your schedules by navigating to Automation \ Jobs. Each scheduled run will be listed within the jobs page.

PowerShell Universal Schedule Jobs

PowerShell Universal Schedule Jobs