Skip to content

Lesson 8 of 13 · 17m

Scheduled Automation That Actually Runs

Register-ScheduledTask builds unattended jobs from action, trigger, principal, and settings — and a gMSA is the identity that never expires.

A scheduled PowerShell job is a Task Scheduler task whose action launches a fresh process with no console, no profile, no user typing passwords. Everything that can go wrong does go wrong there — and it fails silently, because nobody is watching a console that doesn't exist. The task either reports itself correctly or it might as well not run.

Two module names that look interchangeable and aren't

Register-ScheduledJob comes from the PSScheduledJob module — Windows PowerShell 5.1 only, absent in PowerShell 7. It wraps a task that launches powershell.exe and drops job-format output under ~\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs, retrievable with Get-Job/Receive-Job. It exists for job semantics, not scheduling discipline.

Register-ScheduledTask is the ScheduledTasks module — a CDXML layer over the same WMI/Task Scheduler service schtasks.exe and the MMC snap-in drive — and it works in both editions. Build a task in four pieces: New-ScheduledTaskAction (the command), New-ScheduledTaskTrigger (when), New-ScheduledTaskPrincipal (as whom), New-ScheduledTaskSettingsSet (behavior), then Register-ScheduledTask welds them together. This is the API for production automation.

The action line is where tasks die

The canonical action:

New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\ops\report.ps1"'

Every flag earns its place. -NoProfile keeps your $PROFILE — which may load modules, print banners, or Read-Host — out of a non-interactive process. -ExecutionPolicy Bypass skips the consent prompt logic on a headless run (you learned in the two-editions lesson that it's not a security boundary; the task is already trusted code you deployed). -File is load-bearing for exit codes: powershell.exe -File script.ps1 propagates the script's exit N as the process exit code, which Task Scheduler records as LastTaskResult. With -Command you'd have to engineer that propagation yourself, and quoting -Command inside an XML action string is where trailing quotes and embedded variables go to die. -File with a quoted path is simpler and auditable.

And mind 0x41301. Get-ScheduledTaskInfo exposes LastTaskResult as hex — 0x0 success, 0x1 general failure, 0x41301 means the task is still running — a state operators have misread as failure for a decade. The task history event log (Microsoft-Windows-TaskScheduler/Operational, disabled by default on clients) is where 201/107/102 events confirm what actually fired and finished.

Run-as is the credential decision

New-ScheduledTaskPrincipal -UserId 'DOMAIN\svc-report' -LogonType ServiceAccount runs whether or not anyone is logged in — Password logon type stores a credential in the task store for the token to work; S4U avoids storing it but can't reach network resources; Interactive only works while that user is logged on. Running as SYSTEM (-UserId 'S-1-5-18' -LogonType ServiceAccount) gives you local omnipotence for free — and the network identity becomes the machine account (WEB01$), which is actually fine when the share grants that account, and silently broken when it doesn't.

The production answer is a gMSA: New-ScheduledTaskPrincipal -UserId 'CONTOSO\svc-tasks$' -LogonType Password works on Server 2016+/Windows 10+ — no password stored anywhere, AD rotates the secret, the account can reach network resources as itself. Compare with the anti-pattern that keeps appearing in audits: a scheduled task running as a domain admin because "it needs to touch everything," or a human's account that works until their password expires and the task starts failing invisibly at 0x8007052E — Logon failure: unknown user name or bad password — weeks later.

-RunLevel Highest on the principal equals the "run with highest privileges" checkbox: a full, unfiltered admin token. Without it, a task under an admin user runs the filtered token and your Set-ItemProperty HKLM:\ writes land in the void.

Settings are the reliability layer

New-ScheduledTaskSettingsSet is where operational behavior lives, and the defaults are set for laptops, not servers. -StartWhenAvailable catches up runs missed while the box was off. -ExecutionTimeLimit (New-TimeSpan -Hours 2) kills a wedged job instead of letting it hold a file lock forever. -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 5) retries transient failure. -DisallowStartIfOnBatteries $false — servers don't care, but the default battery/idle conditions absolutely do surprise people running tasks on workstations and VMs. -MultipleInstances IgnoreNew stops a second instance from stacking on a still-running first one — combined with -ExecutionTimeLimit, it's how you keep an hourly job from becoming a pile of concurrent wedged processes.

The practice

Task definition belongs in code: the same Register-ScheduledTask block that builds it is the documentation of it. Action runs pwsh.exe -NoProfile -ExecutionPolicy Bypass -File with a fully quoted path; principal is a gMSA or a purpose-built service account, never a human; settings include a time limit and a restart policy; the script ends with exit $code so LastTaskResult is a signal. Then monitor Get-ScheduledTaskInfo and the TaskScheduler Operational log — because the only thing worse than a failing job is a job that hasn't run in three weeks and nobody noticed.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.