Set-ScheduledJobOption

PowerShell cmdlet deep-dive: Set-ScheduledJobOption

Welcome back to Wahmans PowerShell blog! Today we are diving into the Set-ScheduledJobOption cmdlet. This powerful command is used to modify the options of a scheduled job in PowerShell. Whether you’re adjusting how often a job runs, managing the job’s behavior when the computer is on battery, or setting up logging details, this cmdlet has you covered.

What does it do?

As per Microsoft, Set-ScheduledJobOption “Changes the job options of a scheduled job.”

Before you use this cmdlet, make sure you have already created a scheduled job using Register-ScheduledJob. Once your job is created, Set-ScheduledJobOption can help you manage when and how it runs.

Example 1: A Beginner’s Touch

Let’s start simple. Suppose we have a scheduled job named “WeeklyReport” and we want it to wake the computer to run. That’s an easy tweak using Set-ScheduledJobOption:

Set-ScheduledJobOption -Name "WeeklyReport" -WakeToRun $true

Now your job will wake the system if it is asleep when the schedule hits. Useful when automating tasks like reports overnight!

Example 2: Control on Battery Usage

Next, let’s prevent our job “DataSync” from running when the device is on battery power (to conserve resources):

Set-ScheduledJobOption -Name "DataSync" -StartIfOnBatteries $false -StopIfGoingOnBatteries $true

This ensures the job starts only when plugged in and stops if the system switches to battery power.

Example 3: Keep Job History

Managing job logs is critical for debugging. Here’s how you can increase the number of job history entries retained for the “MonitoringJob”:

Set-ScheduledJobOption -Name "MonitoringJob" -RunWithoutNetwork $true -JobDefinition -MaxResultCount 50

This configuration also allows the job to run even when the network is unavailable and stores up to 50 results.

Example 4: Advanced — Combine All the Features

Now, a more advanced example. You want a job called “SystemHealthCheck” to run:

  • When plugged in
  • Wake the computer if asleep
  • Run even if no network is present
  • Retry three times if it fails

Here’s how:

$options = New-ScheduledJobOption -WakeToRun $true -StartIfOnBatteries $false -RunWithoutNetwork $true -RetryInterval (New-TimeSpan -Minutes 5) -RetryCount 3
Set-ScheduledJobOption -Name "SystemHealthCheck" -ScheduledJobOption $options

This gives more resilience to jobs that rely on system uptime and health checks.

Wrapping up

Set-ScheduledJobOption is a versatile cmdlet that fine-tunes the way your scheduled jobs operate in PowerShell. With a combination of power settings, logging, and retry mechanisms, you’re equipped for a wide range of automation tasks.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *