I have a Powershell script that uses the Invoke-WebRequest
command to make a Webservice request that returns an XML structure. I use the parameters MaximumRetryCount
and RetryIntervalSec
to handle any errors that might occur.
The call is embedded into a try/catch logic and looks as follows.
try {
LogWrite "Download starts." 6;
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $url -OutFile $output -MaximumRetryCount 10 -RetryIntervalSec 10
}
catch {
# writing the error message to the log
exit;
}
Recently I found the following error in my log file:
05:00:01 HOSTNAME Download starts.
05:00:09 HOSTNAME An exception was caught: Invoke-WebRequest : No such host is known.At [REDACTED].ps1:xx char:2+ Invoke-WebRequest -Uri $url -OutFile $output -MaximumRetryCount 1 ?+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ / + CategoryInfo : InvalidOperation: (Method: GET, Reques?PowerShell/7.3.6}:HttpRequestMessage) [Invoke-WebRequest], HttpRequestException / + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand /
As you can see there are only a few seconds between the first log message and the exception. Due to the parameters MaximumRetryCount
and RetryIntervalSec
I would have expected that the script performs several retries, but this obviously did not happen.
I am wondering why no retries were performed? I know that I could implement a retry logic on my own, but I would have expected that the retry-parameters take care of that?
I use Powershell version 7.3.9.
According to documentation,
MaximumRetryCount. Specifies how many times PowerShell retries a connection when a failure code between 400 and 599, inclusive or 304 is received
So it only affects HTTP StatusCode responses and does nothing for other errors, like unknown host or when a network is down.