|
1 | 1 | ################################################################################ |
2 | 2 | ## File: Install-PowershellCore.ps1 |
3 | 3 | ## Desc: Install PowerShell Core |
| 4 | +## Supply chain security: checksum validation |
4 | 5 | ################################################################################ |
5 | 6 |
|
6 | 7 | $ErrorActionPreference = "Stop" |
7 | 8 |
|
| 9 | +#region functions |
| 10 | +Function Get-PowerShellCoreHash |
| 11 | +{ |
| 12 | + Param ( |
| 13 | + [Parameter(Mandatory = $True)] |
| 14 | + [string] $Release |
| 15 | +) |
| 16 | + |
| 17 | + $hashURL = "https://github.com/PowerShell/PowerShell/releases/download/v${Release}/hashes.sha256" |
| 18 | + (Invoke-RestMethod -Uri $hashURL).ToString().Split("`n").Where({ $_ -ilike "*PowerShell-${Release}-win-x64.msi*" }).Split(' ')[0] |
| 19 | + |
| 20 | +} |
| 21 | +#endregion |
| 22 | + |
8 | 23 | $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()) |
9 | 24 | $null = New-Item -ItemType Directory -Path $tempDir -Force -ErrorAction SilentlyContinue |
10 | 25 | try { |
|
21 | 36 | $packagePath = Join-Path -Path $tempDir -ChildPath $packageName |
22 | 37 | Invoke-WebRequest -Uri $downloadURL -OutFile $packagePath |
23 | 38 |
|
| 39 | + #region Supply chain security |
| 40 | + Write-Verbose "Performing checksum verification" |
| 41 | + |
| 42 | + $distributor_file_hash = Get-PowerShellCoreHash -Release $release |
| 43 | + $local_file_hash = (Get-FileHash -Path $packagePath -Algorithm SHA256).Hash |
| 44 | + |
| 45 | + if ($local_file_hash -ne $distributor_file_hash) { |
| 46 | + Write-Host "hash must be equal to: ${distributor_file_hash}" |
| 47 | + Write-Host "actual hash is: ${local_file_hash}" |
| 48 | + throw 'Checksum verification failed, please rerun install' |
| 49 | + } |
| 50 | + #endregion |
| 51 | + |
24 | 52 | Write-Verbose "Performing quiet install" |
25 | 53 | $ArgumentList=@("/i", $packagePath, "/quiet") |
26 | 54 | $process = Start-Process msiexec -ArgumentList $ArgumentList -Wait -PassThru |
|
0 commit comments