PowerShell Scheduler cmdlets

A Scheduled Job is a background job that can be started automatically on a one-time or recurring schedule.

  Register-ScheduledJob   Create a new scheduled job
  Unregister-ScheduledJob Delete a scheduled job
  Enable-ScheduledJob     Enable a scheduled job
  Disable-ScheduledJob    Disable a scheduled job
  Get-ScheduledJob        Get scheduled jobs on the local computer
  Set-ScheduledJob        Change the properties of a scheduled job.

  Get-ScheduledJobOption  Get the options of scheduled job
  New-ScheduledJobOption  Create new options for a scheduled job
  Set-ScheduledJobOption  Change the options of a scheduled job

  Add-JobTrigger     Add  a job trigger to a scheduled job
  Get-JobTrigger     Get the job triggers of scheduled jobs
  New-JobTrigger     Create a new job trigger
  Set-JobTrigger     Change the job trigger of a scheduled job

The cmdlets on this page are available in PowerShell 3.0 and greater.

When scheduling a single job you may find it easier to just call the old school SCHTASKS command, for multiple jobs on multiple machines the PowerShell cmdlets will start to show their advantages.
Whichever method is used, the same Scheduled Job will result.

A Scheduled Job can be exported from one computer as an .XML file and imported to a different machine, this can be a good option if you want consistent job settings on multiple machines without having to write any scripts.

Set the owner of a Scheduled Job

There are two main ways to run a scheduled job:

For a group - the scheduled job will run as the logged in user if they are a member of the named group.
Most often this is the group BUILTIN/USERS, every domain user is a member of USERS, so the job will run for the currently logged in user. The scheduled job will thus have access to the same resources as that User Account (file permissions, network access etc.) If no-one is logged in the job will not run.

For a named user, either a local account or a Domain Account - the scheduled job will have access to the same resources as that Account (file permissions, network access etc)

If it is possible to run a job as the already logged-in user, or a local account this will avoid the network traffic of a domain login, which can be significant if hundreds or thousands of workstations all run a scheduled job at the same time.

Service Accounts

The LocalSystem account (NT AUTHORITY\SYSTEM + BUILTIN\Administrators) is a highly privileged account with access to the network and Active Directory. It does not have a user profile and does not allow interactive logins (interacting with the desktop.) While the SYSTEM account was used to run System Services and Scheduled jobs in early versions of Windows NT, this is no longer recommended.

The LocalService account (NT_AUTHORITY\LocalService), is a limited service account that is meant to run standard least-privileged services, it has no ability to access the network as the machine.

The NetworkService account (NT AUTHORITY\NetworkService) is meant to run standard least-privileged services. but still has the right to access the network as the machine.

Both LocalService and NetworkService have minimal authority on the local computer, basically the same authority as an unprivileged user account plus the Logon as a service right, which any service account requires. Because these built-in accounts run multiple services, any additional rights granted to them would pass on to those processes too.
To avoid this, use a dedicated user account (or Managed Service Account) with the desired permissions.

Job Options: Run only when user is logged on

When this option is chosen the running job may be visible to the user, if the same job is set to Run whether user is logged on or not, it will be invisible to any user who happens to be logged on to the machine.

Job Options: Wake the computer to run this task

To wake up, Windows must talk to the BIOS to schedule a wake up. For this to work, you must enable the"Allow wake timers" setting on the current power policy ( Control Panel | Power Options ). Support for this is limited, for example a BIOS written to ACPI v4 will not accept a wake schedule more than 30 days into the future.

This property is essential if you need the task to run on a machine that has PowerSaving enabled.
See also Powercfg.exe -WakeTimers

Job Options: Run task as soon as possible after a scheduled start is missed

This option does not run a missed job immediately, which you might reasonably expect. There is a 10-minute delay. So if a job was missed because the PC was off, it will run exactly 10 minutes after the computer is powered back on.
If multiple jobs are missed, they will all start at the same time: exactly 10 minutes after the computer is powered back on.

This delayed start is not shown in under 'Next Run Time', but will appear under 'Last Run Time' once the job has run.
This option is ignored completely if the job has a "ONE Time" trigger.

Notes

When a scheduled job is running, the environment variable %SESSIONNAME% is not populated:
if ($env:SessionName -eq $null) {Echo 'This is a scheduled job'}

If a Scheduled Job has multiple 'Actions' they will run in order, if one stalls, it will block later tasks from running. If you need tasks to run asynchronously, set them up as separate jobs.

The options described on this page are for the Windows Scheduler 2.0 available in Windows Vista and greater.

Windows 8

The two actions: showMessage and sendEmail are deprecated (use IExecAction with Send-MailMessage as a workaround).

“In order to plan your future wisely, it is necessary that you understand and appreciate your past” ~ Jo Coudert

Related

Task Scheduler error codes
SCHTASKS - Create /Edit a Scheduled Job.
Run with elevated permissions


Copyright © SS64.com 1999-2018
Some rights reserved