@@ -29,11 +29,47 @@ function Invoke-WebRequestWithRetry {
2929 Write-Host " Retry attempt $attempt of $MaxRetries after ${RetryDelay} s delay..."
3030 Start-Sleep - Seconds $RetryDelay
3131 }
32-
33- # Use -Resume to support partial downloads if the connection drops
34- Invoke-WebRequest - Uri $Uri - OutFile $OutFile - TimeoutSec $TimeoutSec - UseBasicParsing
35-
36- Write-Host " Download completed successfully"
32+
33+ Write-Host " Attempt $attempt `: Downloading from $Uri "
34+
35+ # Clean up any existing partial download
36+ if (Test-Path $OutFile ) {
37+ Remove-Item - Force $OutFile - ErrorAction SilentlyContinue
38+ }
39+
40+ # Get expected file size from HTTP headers
41+ $headRequest = Invoke-WebRequest - Uri $Uri - Method Head - UseBasicParsing - TimeoutSec 30
42+ $expectedSize = $null
43+ if ($headRequest.Headers.ContainsKey (' Content-Length' )) {
44+ $expectedSize = [long ]$headRequest.Headers [' Content-Length' ][0 ]
45+ Write-Host " Expected file size: $ ( $expectedSize / 1 MB ) MB"
46+ }
47+
48+ # Download with progress tracking disabled for better performance
49+ $webClient = New-Object System.Net.WebClient
50+ $webClient.DownloadFile ($Uri , $OutFile )
51+ $webClient.Dispose ()
52+
53+ # Verify file exists and has content
54+ if (-not (Test-Path $OutFile )) {
55+ throw " Download completed but file not found at $OutFile "
56+ }
57+
58+ $actualSize = (Get-Item $OutFile ).Length
59+ Write-Host " Downloaded file size: $ ( $actualSize / 1 MB ) MB"
60+
61+ # Verify file size matches expected size
62+ if ($expectedSize -and $actualSize -ne $expectedSize ) {
63+ throw " File size mismatch. Expected: $expectedSize bytes, Got: $actualSize bytes"
64+ }
65+
66+ # Verify file is not corrupted by checking if it's a valid PE executable
67+ $fileBytes = [System.IO.File ]::ReadAllBytes($OutFile )
68+ if ($fileBytes.Length -lt 2 -or $fileBytes [0 ] -ne 0x4D -or $fileBytes [1 ] -ne 0x5A ) {
69+ throw " Downloaded file is not a valid executable (missing MZ header)"
70+ }
71+
72+ Write-Host " Download completed and verified successfully"
3773 return $true
3874 }
3975 catch {
0 commit comments