Lesson 9 of 13 · 15m
Scripts That Report Themselves
Headless automation needs JSONL logs, stream discipline, event log entries, and exit codes — not console noise.
Nobody reads the console output of a scheduled task, because there is no console. A script that only reports success by printing "Done" to a host that doesn't exist has reported nothing. Automation that matters produces three artifacts: a structured log humans and machines can consume, exit codes the scheduler understands, and — for things that actually break — an event or a message that reaches a human before the blast radius does.
Transcripts: what they are and aren't
Start-Transcript -Path C:\ops\logs\report-$(Get-Date -f yyyyMMdd-HHmmss).log -Append captures host output — streams 1-6 as rendered to the console host — into a text file. It's cheap and ubiquitous, which is why every environment has a folder of them. Know the limits. In 5.1, transcripts live in the user's Documents folder by default — pass -Path or -OutputDirectory (7.x added -OutputDirectory; -UseMinimalHeader, which drops the version/header noise, shipped in 6.2). Transcripts record what the host wrote; they miss anything logged purely to a file and duplicate what you already write to your own log. And they don't capture process crashes — a hard-faulting pwsh leaves a truncated file with no closing line.
The right job for a transcript is an audit trail of an interactive or semi-attended run, not the telemetry of headless automation. For headless, you want structure, not console-shaped text.
Structured logs, not prose
A log that gets grepped should be a log that's parseable. The pattern that works: emit one object per run (or per significant event) and append it as a JSON line — ConvertTo-Json -Compress writes single-line objects, and a directory of .jsonl files is grep-able with Select-String, ingestible by any SIEM, and diffable across runs.
[pscustomobject]@{
ts = (Get-Date).ToString('o')
host = $env:COMPUTERNAME
script = 'report.ps1'
result = 'success'
items = $count
elapsed = $sw.Elapsed.TotalSeconds
errors = @($skipped).Count
} | ConvertTo-Json -Compress | Add-Content C:\ops\logs\report.jsonlCompare Select-String -Path report.jsonl -Pattern '"result":"failed"' against a folder of free-text transcripts and the argument makes itself. Prose logs answer "what happened?" once; structured logs answer "which runs failed in March on web*?" forever.
Streams are the API of your script
Write-Verbose and Write-Debug behind [CmdletBinding()] are gated chatter — silent until someone passes -Verbose, free when nobody does. Write-Warning (stream 3) is for non-fatal anomalies a caller might redirect separately (3>warnings.log). Write-Information (stream 6, PS5+) with -Tags is the production channel — -Tags @('alert','disk') survives *> capture and lets a caller filter the records collected via -InformationVariable iv.
Write-Host is the one to exile from automation. In modern PowerShell it writes to stream 6 — capturable but not consumable as output; before PS5 it went straight to the console and no redirection on earth could touch it. Its actual job is display: colors, menus, progress for a human watching a terminal. Anything your caller needs to act on goes to the output stream or a file; anything they might need to know goes to Verbose/Warning/Information. Write-Host output is the single most common reason a scheduled task's transcript is a wall of green text and zero data.
Event logs and escalation
Windows already owns the alerting pipeline — write to it. New-EventLog -LogName Application -Source Ops-Reporting once, then Write-EventLog -LogName Application -Source Ops-Reporting -EventId 9001 -EntryType Warning -Message "..." on every failure. WEF subscriptions, your SIEM's Get-WinEvent agent, or a Register-ObjectEvent-free monitoring rule picks it up. Classic event log writes are 5.1-friendly; New-WinEvent is for manifest-based events if you go that deep.
For reach-a-human urgency, email still works (Send-MailMessage — deprecated but alive), Teams/Slack webhooks (Invoke-RestMethod -Uri $hook -Method Post -Body (@{text=...} | ConvertTo-Json)), or your paging system. The rule isn't the channel — it's that someone learns a job failed before they learn it from a user.
Exit codes are the contract
Task Scheduler reads the process exit code into LastTaskResult. A script that throws without exit may report 0x1; a script that catches everything silently reports 0x0 on a run that did nothing. End the script deliberately: exit 0 success, exit 2 partial, exit 1 failure — and record the same semantics in the JSONL line. robocopy-style native calls need $LASTEXITCODE checked explicitly, since native non-zero exits don't raise PowerShell errors.
The practice
Every automated script ends the same way: JSONL record appended, exit code emitted, event log entry on failure. Start-Transcript when you want an audit trail, Write-Verbose for narrative, Write-Warning for anomalies, Write-Information for telemetry, and Write-Host never. Three weeks later, when you're asked "did that run last night?", the answer is Select-String, not "I think so."
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.