I named a variable $oldHost. The script already had a parameter called -OldHost. Those are the same variable. I know that now.
The script
It’s a verification step in a migration pipeline I run from a Windows machine. After a site has been loaded onto its new server, the script runs a WordPress search-replace over SSH so the database stops mentioning the temporary address and starts using the real domain. It takes the old and new hosts as parameters, because it has to know which machine to SSH into.
Somewhere in the middle I wanted two local variables holding the hostname portion of each URL, stripped of scheme and trailing slash. I called them what seemed obvious.
param(
[string] $OldHost,
[string] $NewHost
)
# ...later in the script
$oldHost = ($currentSiteurl -replace '^https?://','' -replace '/$','')
$newHost = ($targetUrl -replace '^https?://','' -replace '/$','')
PowerShell variable names are case-insensitive. $oldHost and $OldHost are one slot, so that assignment didn’t create anything. It overwrote the parameter.
Where it bit
Every later line that said ssh $NewHost ... now meant “SSH to the domain name”. And before cutover, the domain name still points at the old server.
So the search-replace, intended for the freshly built box, was aimed at the live site’s previous home. Across all tables of the network.
What stopped it? A coincidence. The old servers use a different admin username from the new ones, and the script logs in with the new one. The login failed. That was the whole safety net: a mismatched username on a machine I hadn’t meant to touch.
I found it by reading why a step had failed in an unexpected way, not by anything clever. The error was an authentication failure on a host I’d have sworn I wasn’t connecting to.
The fix is dull
$srFromHost = ($currentSiteurl -replace '^https?://','' -replace '/$','') $srToHost = ($targetUrl -replace '^https?://','' -replace '/$','')
Rename. Pick local names that can’t collide with a parameter however you capitalise them. I now give script-local working variables a short prefix for the step they belong to, which is ugly and has already earned its keep.
The nasty part is that nothing warns you. Set-StrictMode won’t, because the variable exists. No linter I use flags it either, because it’s valid code that does what it says. The language is behaving as documented. It just isn’t behaving as I’d assumed.
The second bug in the same function
While I was in there, the same run produced a URL like www.www.example.com on the new box.
The script does the replace twice: once on full URLs, once on bare hostnames to catch subsites that map their own domains. When the target was just the source with www. stuck on the front, the bare hostname example.com also matched inside every existing www.example.com, and each one became www.www.example.com.
The guard is short.
$skipPass2 = ($srToHost -ne $srFromHost) -and
$srToHost.EndsWith($srFromHost, [StringComparison]::OrdinalIgnoreCase)
If the target is merely the source plus a prefix, skip the hostname-only pass. The full-URL pass already did the job. That one went wrong only on the new box, which I could throw away and reload, so it cost me a re-import and nothing worse.
What I’d say to myself
Case-insensitivity is a lovely feature until two things with the same name in different capitals mean different things to you. If a script takes parameters and also does any assigning, grep it for each parameter name ignoring case before you trust it. That takes a minute.
The login that failed had no idea it was saving me. I’d rather not rely on it again.
