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?
Keep reading
Related articles from the SudoSecurity blog.
A Fake GTA 6 ISO Didn’t Install a Game. It Installed a Wiper.
A fake GTA 6 ISO used a believable installer error to cover remote-access malware, browser-data theft, and destructive file wiping. The “License not found” message wasn’t the crack failing. It was there so the victim would stop looking.
BlueHammer: The Unpatched Windows Zero-Day That Weaponizes Microsoft Defender Against Your Own System
BlueHammer is an unpatched, publicly released Windows local privilege escalation exploit that chains Microsoft Defender's update workflow, Volume Shadow Copy, Cloud Files callbacks, and opportunistic locks to reach NT AUTHORITY\SYSTEM from a standard user account.
Running a Minecraft Network: Four JVMs, One Box, and the Settings That Lie to You
Four servers on one host means four JVMs each sizing its thread pools for a machine it does not own. The measured cost of that, the systemd ordering that guarantees nothing, and the throttle that switches itself back on when your forwarding breaks.
Building a Minecraft Network: Four Servers Pretending to Be One Place
A proxy, a lobby, an SMP and a PvP arena is four JVMs that know nothing about each other. What actually has to be shared, why your permissions don't follow players, and the two LuckPerms defaults that silently share nothing at all.