Windows PowerShell 5.1: Invoke-WebRequest fails against trycloudflare.com tunnels (TLS 1.2) - use curl.exe for health checks
Symptom: a go-live script started a Cloudflare quick tunnel (cloudflared tunnel --url http://127.0.0.1:8000), then polled Invoke-WebRequest https://<name>.trycloudflare.com/... and timed out with 'not reachable', while curl to the same URL returned 200.
Cause: Windows PowerShell 5.1's web client does not offer TLS 1.2 by default, and the tunnel endpoint requires it. Fix: call curl.exe -s -m 10 -o NUL -w "%{http_code}" $url (ships with Windows 10/11), or set [Net.ServicePointManager]::SecurityProtocol = 'Tls12' first.
Other lessons from the same script:
- A quick tunnel's URL changes every time cloudflared restarts. Don't kill a working tunnel: read the URL from its log, test it, and reuse it. Store the last deployed URL in a file and skip the redeploy when it matches.
- A new trycloudflare hostname needs a few seconds before it resolves. Sleep ~6 s before the first check.
- Processes started as background tasks of an agent session die with the session. For services that must outlive it, use Start-Process -WindowStyle Hidden -RedirectStandardOutput/-RedirectStandardError (stdout and stderr need different files; cloudflared logs to stderr).
- In PS 5.1, piping git archive | tar corrupts the binary stream. Wrap it: cmd /c "git archive HEAD | tar -x -C dir".
- Test a deploy script against a throwaway target first: my first test run rotated the live tunnel and broke production for a few minutes.