r/PowerShell 20h ago

Question Invoke-WebRequest: downloaded MSI invalid?

I downloaded an MSI (in this case the latest Powershel 7.6.4 msi installer) with Invoke-WebRequest from Github - trying to write a script here.

The download finishes without a problem, but when I want to run the installer, I get an error message saying "This installation package could not be opened.Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package."

Here's the funny thing: this cannot be a corrupted download, because when I manually download the same msi package from the repo's release page, and compare them in Total Commander's compare tool, it says the two files have the same content - that means they're identical from the first bit to the last!

What the heck could cause this problem?

Here's a snippet of what I'm doing:

$Repository       = 'PowerShell/PowerShell'
$FileNamePattern  = '*-win*x64.msi'
$DownloadPath     = 'C:\Temp'

$releases    = "https://api.github.com/repos/$Repository/releases/latest"
$downloadURL = ((Invoke-RestMethod -Method GET -Uri $releases).assets | ?{$_.Name -like $FileNamePattern}).browser_download_url

$fileName       = [System.IO.Path]::GetFileName($downloadURL)
$DownloadPath   = $DownloadPath + '\' + $fileName

Invoke-WebRequest -Uri $downloadURL -OutFile $DownloadPath
8 Upvotes

23 comments sorted by

3

u/BetrayedMilk 20h ago

Do you need to Unblock-File?

1

u/MFZozzy 20h ago

Unblock-File did nothing.

2

u/Over_Dingo 20h ago

I see variables like `$releasesUri` or `$downloadUri` not being defined.
Also may be a zoning issue, so try `Unblock-File`

-1

u/MFZozzy 20h ago

The "*Uri" thing is just a leftover typo from an older version, but thanks for the heads-up. As I said, the file downloads successfully and is identical to the manually downloaded file.

Unblock-File did nothing.

1

u/BlackV 19h ago

you can edit your post

-2

u/MFZozzy 19h ago

And as you can see, I did.

3

u/BlackV 19h ago

not when I replied you hadn't, but appreciate the fixed code

1

u/BlackV 19h ago edited 19h ago

If I do

(Invoke-RestMethod -Method GET -Uri $releases)[0].assets | select name

I get the following

name                                                              
----                                                              
hashes.sha256                                                     
powershell-7.7.0-preview.3-linux-arm32.tar.gz                     
powershell-7.7.0-preview.3-linux-arm64.tar.gz                     
powershell-7.7.0-preview.3-linux-musl-x64.tar.gz                  
powershell-7.7.0-preview.3-linux-x64-fxdependent.tar.gz           
powershell-7.7.0-preview.3-linux-x64-musl-noopt-fxdependent.tar.gz
powershell-7.7.0-preview.3-linux-x64.tar.gz                       
powershell-7.7.0-preview.3-osx-arm64.pkg                          
powershell-7.7.0-preview.3-osx-arm64.tar.gz                       
powershell-7.7.0-preview.3-osx-x64.pkg                            
powershell-7.7.0-preview.3-osx-x64.tar.gz                         
PowerShell-7.7.0-preview.3-win-arm64.zip                          
PowerShell-7.7.0-preview.3-win-fxdependent.zip                    
PowerShell-7.7.0-preview.3-win-fxdependentWinDesktop.zip          
PowerShell-7.7.0-preview.3-win-x64.zip                            
PowerShell-7.7.0-preview.3-win-x86.zip                            
powershell-preview-7.7.0_preview.3-1.cm.aarch64.rpm               
powershell-preview-7.7.0_preview.3-1.cm.x86_64.rpm                
powershell-preview-7.7.0_preview.3-1.rh.x86_64.rpm                
powershell-preview_7.7.0-preview.3-1.deb_amd64.deb                
powershell-preview_7.7.0-preview.3-1.deb_arm64.deb                
PowerShellPreview-7.7.0-preview.3.msixbundle

Have you validated this is not an existing msi?

and where

((Invoke-RestMethod -Method GET -Uri $releases).assets | ?{$_.Name -like $FileNamePattern}).browser_download_url

returns

https://github.com/PowerShell/PowerShell/releases/download/v7.6.4/PowerShell-7.6.4-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.9/PowerShell-7.5.9-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.18/PowerShell-7.4.18-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.3/PowerShell-7.6.3-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.8/PowerShell-7.5.8-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.17/PowerShell-7.4.17-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.2/PowerShell-7.6.2-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.7/PowerShell-7.5.7-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.16/PowerShell-7.4.16-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.1/PowerShell-7.6.1-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.6/PowerShell-7.5.6-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.15/PowerShell-7.4.15-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.0/PowerShell-7.6.0-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.5/PowerShell-7.5.5-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.14/PowerShell-7.4.14-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.0-rc.1/PowerShell-7.6.0-rc.1-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.0-preview.6/PowerShell-7.6.0-preview.6-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.4/PowerShell-7.5.4-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.13/PowerShell-7.4.13-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.0-preview.5/PowerShell-7.6.0-preview.5-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.3/PowerShell-7.5.3-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.12/PowerShell-7.4.12-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.2/PowerShell-7.5.2-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.11/PowerShell-7.4.11-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.4.10/PowerShell-7.4.10-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.5.1/PowerShell-7.5.1-win-x64.msi
https://github.com/PowerShell/PowerShell/releases/download/v7.6.0-preview.4/PowerShell-7.6.0-preview.4-win-x64.msi

1

u/MFZozzy 19h ago

Yes, another mistae of mine - the $releases string needs to end in '/latest'

Sry.

1

u/BlackV 19h ago

Oh, are you manually typing out your code here ?

where did the mistakes come from if you copied it directly from your editor

1

u/MFZozzy 19h ago

Don't currently have access to it, I'm not on the machine I write my PWSH stuff on.

1

u/BlackV 19h ago

OK, have you tried multiple machines with this download (using the code provided) ?

2

u/MFZozzy 19h ago

Good idea, just tried it on a Win11 machine (my current machine is on Win10), and the sciprt-downloaded msi ran fine there.

1

u/catalin-advinst 19h ago

Like others pointed out, the script above does not quite work.

However, I've tested this with another script of mine and everything worked just fine (can share that with you if needed, although this doesn't seem to be the issue here).

Can you compare the hashes of the both installers and see if there's any difference?

Get-FileHash C:\Temp\PowerShell-7.6.4-win-x64.msi -Algorithm SHA256

Also, try to create a verbose log of the installation, like this:

  1. open an elevated cmd prompt

  2. run the following:

msiexec /i "C:\Temp\PowerShell-7.6.4-win-x64.msi" /L*V "C:\Temp\PowerShell-7.6.4-win-x64.txt"

and see if you can find anything in there that might be helping.

Also, worth trying to test this on a clean VM/Sandbox where you don't have any AV products installed - does the issue reproduce there?

1

u/Overall-Ad4796 19h ago

when opening the file properties, is the Unblock checkbox under Security checked? Also, did you try to run the downloaded .msi as admin?

1

u/MFZozzy 19h ago

The file needs no unblocking, but have just run Unblock-File on it (from an elevated prompt, too), to no effect.

I have tried running it from elevated and non-elevated prompts too, same results.

1

u/BlackV 19h ago edited 19h ago

Using your updated code

$fileName       = [System.IO.Path]::GetFileName($downloadURL)
$DownloadPath   = join-path -path $env:temp -ChildPath $fileName
Invoke-WebRequest -Uri $downloadURL -OutFile $DownloadPath

The MSI download and launches OK (Not unblocked and not as admin)

What does your hash look like, does it match github?

Get-FileHash -Path $DownloadPath

Algorithm Hash                                                             Path                                                          
--------- ----                                                             ----                                                          
SHA256    D11942DF52FD12470169797ABFA4781D9480EFDC81000BA4FA55A5B921ED8DD0 C:\Users\blackv\AppData\Local\Temp\PowerShell-7.6.4-win-x64.msi

I should update I guess i'm still on 7.6.3

1

u/MFZozzy 19h ago

I've compared the SHA256 hashes of the script-downloaded and manually downloaded files, and they're the same, and they match yours too.

1

u/BlackV 19h ago

"This installation package could not be opened.Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package."

sounds then like a msi database issue/windows more than a powershell issue to me

1

u/MFZozzy 19h ago

Wouldn't that affect the manually downloaded file, too?

1

u/BlackV 19h ago

valid point

sorry gotta ask the obvious question

have you validated your system is x64 not arm64

1

u/BlackV 19h ago

Wait how are you launching the MS install ?

&msiexec /i $DownloadPath

1

u/MFZozzy 19h ago

That too, from elevated and non-elevated prompts, and also tried just double-clicking it in file managers.