Invoke-WebRequest sometimes does not download the complete zip or msi file

Hi,

I have external repository from where I need to download my installation files. They are in zip or msi format - for both I noticed that SOMETIMES (about 50%) it does not download them properly.

Invoke-WebRequest -Headers @{‘X-JFrog-Art-Api’=$API_KEY} “$COMPONENT/PowerShell_7_0_0.zip” -OutFile “$DOWNLOAD_PATH\PowerShell_7_0_0.zip”

Because of that my file is not useful, it’s invalid.

I know I can check it “checksum” and compare it with expected value:

(Get-FileHash -Algorithm SHA256 C:\Octopus\Applications<span style=“font-size: 10pt;font-family: ‘courier new’, courier, monospace”>PowerShell_7_0_0.zip).Hash

But isn’t there some better way how I can overcome this? Maybe some built-in TimeOut or Retry for Invoke-WebRequest… or maybe to use some other method… I do not know…

But this is really a big issue which I suppose someone already encountered and resolved it…

I will appreciate very much your assistance. Thanks

 

I’ve used the native .Net webclient in the past to download files. This following script will hopefully help;

 

# set Download path
$DOWNLOAD_PATH = "C:\Temp"

API Key

$API_KEY = ‘yourAPIkey’

URL to file

$COMPONENT = “https://someurl.com/filestore/

file to download

$download_file = “PowerShell_7_0_0.zip”

set up the .net webclient

$Wget = New-Object System.Net.WebClient

add headers

$Wget.Headers[“X-JFrog-Art-Api”] = $API_KEY

download the file

$Wget.DownloadFile(“$COMPONENT/$download_file” , “$DOWNLOAD_PATH$download_file”)