Using invoke-webRequest turns out to be very slow, especially noticeable when downloading large files, because invoke-webRequest reports every single byte in the progress bar.
Error message: The request was aborted: Could not create SSL/TLS secure channel.
The error message The request was aborted: Could not create SSL/TLS secure channel. indicates (sometimes?, always?) that (at least?) TLS 1.2 should be used. TLS 1.2 can be enabled with
PS C:\> invoke-webRequest https://somewhere.xy/file.txt -outFile file.txt
Invoke-WebRequest :
Firewall Authentication
You must authenticate to use this service.
…
Using -useBasicParsing is necessary on systems where IE is not installed or configured (for example after a fresh installation of Windows where Internet Explorer was not launched for a first time, or on a Server Core installation of Windows Server).
On such systems, invoke-webRequest throws the error message
The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.
On a non-Windows Server machine, the following modification in the registry allows to use invoke-webRequest without starting Internet Explorer:
Sometimes, trying to execute invoke-webRequest results in the error message The underlying connection was closed: An unexpected error occurred on a send.
-skipHttpErrorCheck is a switch-parameter that was added in PowerShell 7. With this flag, the invoke-webRequest won't throw an exception if the HTTP status indicates an unsuccessful request.
Test
#
# Inspired by https://stackoverflow.com/a/19122582/180275
#
set-strictMode -version latest
function get-url {
param (
[string] $url
)
if ($psVersionTable.psVersion.major -ge 7) {
$res = invoke-webRequest -skipHttpErrorCheck $url
}
else {
$res = try {
invoke-webRequest $url
}
catch {
$_.exception.response
}
}
write-host ""
write-host " Status for ${url}: $($res.statusCode)"
write-host " type of `$res: $($res.GetType().FullName)"
}
write-host "psVersion: $($psVersionTable.psVersion)"
get-url https://google.com/
get-url https://google.com/does-not-exist