Invoke-WebRequest -TimeoutSec raises a generic exception without confirming cancellation
0 reputation · 26 Dec 2022, 02:12 UTC
Goal: determine whether a timeout from Invoke-WebRequest or Invoke-RestMethod in PowerShell 7 can be distinguished from other web-request failures, and whether the underlying HTTP request stops when the client-side timeout elapses.
-TimeoutSec bounds the client wait, but the timeout surfaces as a wrapped task or WebRequest exception rather than a distinct PowerShell timeout error. The remote request may continue until the server observes the closed connection, so catching the exception does not prove server-side work stopped. Windows PowerShell 5.1 may behave differently from PowerShell 7.
Can script authors reliably isolate the timeout exception type? Does the exception indicate whether the outbound request was canceled? What confirms the remote endpoint stopped processing after the client timeout?