Skip to content

Latest commit

 

History

54 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OZOTaskScheduler PowerShell Module

Description

OZOTaskScheduler provides a lightweight PowerShell interface for managing Windows Task Scheduler tasks. It is designed to create, update, enable, disable, export, and remove scheduled tasks using simple function calls and JSON task definitions.

Installation

This module is published to Microsoft's PowerShell Gallery. Run the following command in an Administrator PowerShell session:

Install-Module OZOTaskScheduler

Usage

Import the module in your script or console:

Import-Module OZOTaskScheduler

Functions

Classes

JSON Definition

New-OZOScheduledTask and Set-OZOScheduledTask expect a task expressed as a JSON dictionary. The following example shows a Scheduled task with three schedule entries:

{
    "Name":"Example Scheduled Task",
    "Script":"C:\\Temp\\OZOTaskScheduler-ScheduledTask-Example.ps1",
    "Parameters":"",
    "Directory":"C:\\Temp",
    "Disabled":true,
    "Settings":{
        "AllowDemandStart":true,
        "AllowHardTerminate":true,
        "AllowStartOnRemoteAppSession":true,
        "Compatibility":"Win8",
        "DeleteExpiredTaskAfter":"PT0S",
        "DisallowStartIfOnBatteries":false,
        "DontStopIfGoingOnBatteries":true,
        "ExecutionTimeLimit":"PT0S",
        "Hidden":false,
        "IdleSettings":{
            "StopOnIdleEnd":false,
            "RestartOnIdle":false
        },
        "MultipleInstances":"IgnoreNew",
        "Priority":"Normal",
        "RunOnlyIfNetworkAvailable":false,
        "WakeToRun":false
    },
    "AtLogon":false,
    "AtReboot":true,
    "Once":true,
    "OnceDateTime":{
        "DateTime":"2099-12-31T09:00:00",
        "RandomDelay":0
    },
    "Scheduled":true,
    "Schedules":[
        {
            "WeekDay":"Monday",
            "StartTime":"8:00 AM",
            "RandomDelay":0
        },
        {
            "WeekDay":"Wednesday",
            "StartTime":"8:00 AM",
            "RandomDelay":0
        },
        {
            "WeekDay":"Friday",
            "StartTime":"8:00 AM",
            "RandomDelay":0
        }
    ]
}

The following example shows an AtLogon task:

{
    "Name":"Example AtLogon Task",
    "Script":"C:\\Temp\\OZOTaskScheduler-AtLogonTask-Example.ps1",
    "Parameters":"",
    "Directory":"C:\\Temp",
    "Disabled":true,
    "Settings":{
        "AllowDemandStart":true,
        "AllowHardTerminate":true,
        "AllowStartOnRemoteAppSession":true,
        "Compatibility":"Win8",
        "DeleteExpiredTaskAfter":"PT0S",
        "DisallowStartIfOnBatteries":false,
        "DontStopIfGoingOnBatteries":true,
        "ExecutionTimeLimit":"PT0S",
        "Hidden":false,
        "IdleSettings":{
            "StopOnIdleEnd":false,
            "RestartOnIdle":false
        },
        "MultipleInstances":"IgnoreNew",
        "Priority":"Normal",
        "RunOnlyIfNetworkAvailable":false,
        "WakeToRun":false
    },
    "AtLogon":true,
    "AtReboot":false,
    "Once":false,
    "OnceDateTime":{},
    "Scheduled":false,
    "Schedules":[]
}
Key Description
Name The name of the scheduled task.
Script The full path to the script or program to run.
Parameters Parameters for the script or program.
Directory The working directory for the task.
Disabled Determines whether the task is disabled when created. Allowed values are true and false.
Settings A dictionary of Task Scheduler settings. See Settings, below.
Scheduled Determines whether the task runs on one or more weekly schedules. Allowed values are true and false. May be combined with Once and AtReboot. If combined with AtLogon, the AtLogon trigger is ignored.
Schedules The schedule definitions for Scheduled tasks. See Schedules, below.
Once Determines whether the task runs once at the date and time in OnceDateTime. Allowed values are true and false. May be combined with Scheduled and AtReboot. If combined with AtLogon, the AtLogon trigger is ignored.
OnceDateTime The one-time trigger definition. Required when Once is true; use an empty object when Once is false. See OnceDateTime, below.
AtReboot Determines whether the task runs at startup/reboot. Allowed values are true and false. May be combined with Scheduled and Once. If combined with AtLogon, the AtLogon trigger is ignored.
AtLogon Determines whether the task runs at user logon. Allowed values are true and false. The trigger is created only when Scheduled, Once, and AtReboot are all false.

Settings is a dictionary containing Task Scheduler settings:

{
    "AllowDemandStart":true,
    "AllowHardTerminate":true,
    "AllowStartOnRemoteAppSession":true,
    "Compatibility":"Win8",
    "DeleteExpiredTaskAfter":"PT0S",
    "DisallowStartIfOnBatteries":false,
    "DontStopIfGoingOnBatteries":true,
    "ExecutionTimeLimit":"PT0S",
    "Hidden":false,
    "IdleSettings":{
        "StopOnIdleEnd":false,
        "RestartOnIdle":false
    },
    "MultipleInstances":"IgnoreNew",
    "Priority":"Normal",
    "RunOnlyIfNetworkAvailable":false,
    "WakeToRun":false
}
Key Description
AllowDemandStart Determines whether the task can be started on demand (manually or by another program). Allowed values are true and false. Defaults to true.
AllowHardTerminate Determines whether the task can be terminated by ending its process. Allowed values are true and false. Defaults to true.
AllowStartOnRemoteAppSession Determines whether the task can start when launched from a Remote Desktop/RemoteApp session. Allowed values are true and false. Defaults to true.
Compatibility Task compatibility mode. Allowed values are At, V1, Vista, Win7, and Win8. Defaults to Win8.
DeleteExpiredTaskAfter The amount of time to wait after the task expires before Task Scheduler deletes it, expressed as an ISO 8601 duration (for example, PT0S or P30D). Omit to never delete the task automatically.
DisallowStartIfOnBatteries Determines whether the task is prevented from starting when the computer is running on battery power. Allowed values are true and false. Defaults to true.
DontStopIfGoingOnBatteries Determines whether a running task keeps running after the computer switches to battery power. Allowed values are true and false. Defaults to false.
ExecutionTimeLimit The maximum amount of time the task is allowed to run, expressed as an ISO 8601 duration (for example, PT72H, or PT0S for no limit). Defaults to PT72H.
Hidden Determines whether the task is hidden in the Task Scheduler UI. Allowed values are true and false. Defaults to false.
IdleSettings Idle-related settings for the task. See IdleSettings, below.
MultipleInstances Determines how Task Scheduler handles multiple simultaneous instances of the task. Allowed values are IgnoreNew, Parallel, and Queue. Defaults to IgnoreNew.
Priority The task's process priority. Accepts an integer from 0 (highest) to 10 (lowest), or the friendly value Normal (equivalent to 7). Defaults to 7.
RunOnlyIfNetworkAvailable Determines whether the task only runs when a network connection is available. Allowed values are true and false. Defaults to false.
WakeToRun Determines whether the computer is woken from sleep to run the task. Allowed values are true and false. Defaults to false.

IdleSettings is a dictionary containing idle settings:

{
    "StopOnIdleEnd":false,
    "RestartOnIdle":false
}
Key Description
StopOnIdleEnd Determines whether the task stops if the idle condition ends before the task completes. Allowed values are true and false. Defaults to true.
RestartOnIdle Determines whether the task restarts the next time the computer becomes idle, if it was stopped because the idle condition ended. Allowed values are true and false. Defaults to false.

Schedules is a list of dictionaries. Each dictionary should contain a WeekDay, StartTime, and RandomDelay value in seconds. Example:

[
    {
        "WeekDay":"Monday",
        "StartTime":"8:00 AM",
        "RandomDelay":0
    },
    {
        "WeekDay":"Wednesday",
        "StartTime":"8:00 AM",
        "RandomDelay":0
    },
    {
        "WeekDay":"Friday",
        "StartTime":"8:00 AM",
        "RandomDelay":0
    }
]
Key Description
WeekDay The day of the week to run the task. Allowed values are Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
StartTime The start time for the task in HH:MM AM/PM format.
RandomDelay The number of seconds to randomize the start time. Allowed range is 0-3600 seconds.

OnceDateTime is a dictionary containing one date/time trigger definition:

{
    "DateTime":"2099-12-31T09:00:00",
    "RandomDelay":0
}
Key Description
DateTime The date and time for the one-time trigger. Use an ISO 8601 value. The value must not be in the past.
RandomDelay The number of seconds to randomize the start time. Allowed range is 0-3600 seconds.

Generating a Compressed JSON String

You can define your JSON in any text editor and save it as a file, for example OZOTaskScheduler-ScheduledTask-Example.json and OZOTaskScheduler-AtLogonTask-Example.json, then convert the file to a compressed JSON string with Convert-OZOJsonFileToString:

Convert-OZOJsonFileToString -Path C:\Temp\OZOTaskScheduler-ScheduledTask-Example.json
{"Name":"Example Scheduled Task","Script":"C:\\Temp\\OZOTaskScheduler-ScheduledTask-Example.ps1","Parameters":"","Directory":"C:\\Temp","Disabled":true,"Settings":{"AllowDemandStart":true,"AllowHardTerminate":true,"AllowStartOnRemoteAppSession":true,"Compatibility":"Win8","DeleteExpiredTaskAfter":"PT0S","DisallowStartIfOnBatteries":false,"DontStopIfGoingOnBatteries":true,"ExecutionTimeLimit":"PT0S","Hidden":false,"IdleSettings":{"StopOnIdleEnd":false,"RestartOnIdle":false},"MultipleInstances":"IgnoreNew","Priority":"Normal","RunOnlyIfNetworkAvailable":false,"WakeToRun":false},"AtLogon":false,"AtReboot":true,"Once":true,"OnceDateTime":{"DateTime":"2099-12-31T09:00:00","RandomDelay":0},"Scheduled":true,"Schedules":[{"WeekDay":"Monday","StartTime":"8:00 AM","RandomDelay":0},{"WeekDay":"Wednesday","StartTime":"8:00 AM","RandomDelay":0},{"WeekDay":"Friday","StartTime":"8:00 AM","RandomDelay":0}]}

Encapsulate the resulting compressed JSON string in single quotes (') so it can be used as the value for JsonString:

'{"Name":"Example Scheduled Task","Script":"C:\\Temp\\OZOTaskScheduler-ScheduledTask-Example.ps1","Parameters":"","Directory":"C:\\Temp","Disabled":true,"Settings":{"AllowDemandStart":true,"AllowHardTerminate":true,"AllowStartOnRemoteAppSession":true,"Compatibility":"Win8","DeleteExpiredTaskAfter":"PT0S","DisallowStartIfOnBatteries":false,"DontStopIfGoingOnBatteries":true,"ExecutionTimeLimit":"PT0S","Hidden":false,"IdleSettings":{"StopOnIdleEnd":false,"RestartOnIdle":false},"MultipleInstances":"IgnoreNew","Priority":"Normal","RunOnlyIfNetworkAvailable":false,"WakeToRun":false},"AtLogon":false,"AtReboot":true,"Once":true,"OnceDateTime":{"DateTime":"2099-12-31T09:00:00","RandomDelay":0},"Scheduled":true,"Schedules":[{"WeekDay":"Monday","StartTime":"8:00 AM","RandomDelay":0},{"WeekDay":"Wednesday","StartTime":"8:00 AM","RandomDelay":0},{"WeekDay":"Friday","StartTime":"8:00 AM","RandomDelay":0}]}'

Logging

When available, messages are written to the One Zero One event provider. Otherwise, events are written to the Microsoft-Windows-PowerShell provider as Information events with event ID 4100.

Notes

This module requires Administrator privileges.

License

This module is licensed under the GNU General Public License (GPL) version 2.0.

Acknowledgements

Special thanks to my employer, Sonic Healthcare USA, who supports the growth of my PowerShell skillset and enables me to contribute portions of my work product to the PowerShell community. Thanks also to GitHub Copilot (Claude Sonnet 5), a co-author of this module, for pairing on design reviews, the Once/Settings feature work, GetExistingTask(), and the integration test suite.

About

A lightweight PowerShell interface for managing Windows Task Scheduler tasks.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages