I know it's not exactly what you need, since with this approach you are basically setting a timeout for the whole call to be performed:
- build client stuff
- resolve the address on the DNS
- eventually make the handshake if you are using a secure connection
- send the request
- wait for the response
- build all the objects in which the response is wrapped
BUT, if you just need to set an approximative timeout, considering that in most common cases most of the time is spent on the points 4 and 5, with a Start-Job
+ Wait-Job -Timeout
+ Receive-Job
you should get the job done:
$job = Start-Job -ScriptBolck { Invoke-WebRequest "https://www.example.com/longrunningtask"}
# Wait for the termination of the job, or 60s, whichever occurs first
Wait-Job -Timeout 60
$response = Receive-Job $job