Skip to content

Lesson 13 of 13 · 14m

What Actually Takes Down Production

The -eq filter trap, iex injection, junction traversal, encoding rewrites, and the tasks that ran the wrong binary.

Nobody's automation outage was caused by a feature they didn't know. It was caused by a -eq that behaved like a filter, a junction point nobody noticed, a task that ran the wrong binary, or a retry loop that re-sent the emails. These are the failures that have taken down real systems, and they all have the same shape: the code did exactly what it was told, which was not what anyone meant.

The -eq that filters

'x' -eq 'x' is a comparison. 'x' -eq @('x','y','z') is still a comparison — and it's $false. But @('x','y','z') -eq 'x' is a filter: it returns ('x'). Put a collection on the left of -eq and PowerShell switches from "are these equal" to "give me the matching elements." The production script that writes if ($svc.Status -eq 'Running') works for a year, then $svc becomes a collection of one service named twice and the if silently never fires — the comparison returned an array, which is truthy when non-empty and falsy when empty, so the logic inverts itself depending on data. The disciplined idiom is $null -eq $x for null checks (collection-on-left can never return $null) and .Contains()/Select-Object when you actually want membership.

Invoke-Expression is a bug that ships

Invoke-Expression $userInput executes whatever string it gets — and the string came from a user, an API, a config file, or a colleague's Slack paste. iex on attacker-influenced text is code injection with extra steps, and the fix is almost never "sanitize the string": it's to not build code from data. The call operator & runs a command, not an expression: & $exe -ArgumentList $a, $b treats arguments as arguments, full stop. Parameterized cmdlet calls, switch dispatch tables, and Get-Command + & cover every legitimate use case iex ever pretended to fill. The legitimate iex patterns — reading config that is code, bootstrapping module text — are exactly the ones that should make you stop and ask why a config file contains code.

Junctions, encodings, and the file you didn't mean

Remove-Item -Recurse -Force on a directory tree containing reparse points has followed junctions into their targets on more than one Windows build — the fix landed and the behavior still surprises people on 5.1 era systems. cmd /c rmdir /s /q deletes the junction without traversing it; verify what -Recurse does on your target build before aiming it at anything with links. The same class of bug: Get-Content big.log | Where-Object {...} > big.log reads and writes the same file — > opens for write before the pipeline finishes reading. Temp file, or Set-Content with -Raw input materialized first.

Encoding is the second silent rewrite. > and Out-File write UTF-16LE in Windows PowerShell 5.1 and UTF-8-no-BOM in PowerShell 7 — the same script produces different bytes under each edition. Set-Content writes ANSI-default in 5.1 (Default encoding = the system codepage), which mangles any non-ASCII text; Export-Csv writes ASCII in 5.1. cmd.exe's > writes OEM. Any pipeline that produces a file consumed by another tool must pin -Encoding utf8 explicitly, or you'll spend an afternoon discovering your CSV has a BOM the parser rejects — or no BOM and the parser interprets UTF-8 as ANSI.

The task that "ran" but didn't

Two recurring scheduled-task disasters from the earlier lessons, colliding in prod. First: powershell.exe instead of pwsh.exe in the action — the script is 7-only syntax (??, -Parallel), it parses under 5.1 until it doesn't, and LastTaskResult is 0x1 with no transcript because -NoProfile also suppressed your error-reporting shim. Second: the task runs as a service account, the script does Export-CliXml-decrypt as a different account, the DPAPI scope fails, the credential is $null, the Invoke-Command fails, and the task "succeeds" because the script swallowed the error. Neither is exotic. Both are configuration, not code — and both were invisible because the monitoring watched "did it run" rather than "did it write the expected output row."

Strict mode and the uninitialized variable

Set-StrictMode -Version Latest makes $undefinedVariable throw instead of returning $null. That's a feature — until the script that ran fine for two years hits a path where $results was never populated because the if guard silently skipped it, and now the nightly job dies. Strict mode is worth turning on for new scripts; retrofitting it into production automation is a breaking change you deploy yourself. Same class: $ErrorActionPreference='Stop' converting every non-terminating error into a kill — a -ErrorVariable collector that was the whole point of the loop now throws on the first locked file.

The practice

The checklist that prevents these: -eq gets a scalar left side or explicit .Contains()/$null -eq; no iex on anything a human typed; -Recurse tested on the real filesystem with junctions present; -Encoding pinned on every file write; task actions name the literal binary and the run-account owns the credential file; exit codes carry the semantics, not "it ran." Mass changes go through -WhatIf first, then one host, then -ThrottleLimited waves. Production doesn't fail on the thing you didn't know — it fails on the thing that looked right.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.