Invoke-RestMethod Cancellation Issues

by May 31, 2023

Both Invoke-WebRequest and Invoke-RestMethod are simple-to-use cmdlets to download information from the web. For example, this simple code can query the registration information for any public IP address and return its registered owner:

$IPAddress = '51.107.59.180'
Invoke-RestMethod -Uri "http://ipinfo.io/$IPAddress/json" -UseBasicParsing
 
ip       : 51.107.59.180
city     : Zürich
region   : Zurich
country  : CH
loc      : 47.3667,8.5500
org      : AS8075 Microsoft Corporation
postal   : 8090
timezone : Europe/Zurich
readme   : https://ipinfo.io/missingauth   
 

If something goes wrong while your code communicates with the internet, in Windows PowerShell there was a hard-coded timeout of 300 seconds. So even if your router went down, your PowerShell script would eventually return.

In PowerShell 7, the inner workings of both cmdlets changed significantly. They currently do not have a connection timeout anymore so if your connection breaks, your script will hang forever.

Fortunately, this bug is currently being resolved and will be fixed in one of the upcoming PowerShell 7 updates.


Tweet this Tip! Tweet this Tip!