Validate bootstrap PowerShell and select its MSI installer

This commit is contained in:
Dandaedre 2026-11-21 11:06:16 +01:00
parent 07a71e0c98
commit 7d44cd9839
3 changed files with 70 additions and 4 deletions

View File

@ -168,3 +168,5 @@ open until it is run on disposable machines with the required account setups.
## PowerShell prerequisite reliability
- [x] Remove PowerShell from routine software updates and block runtime replacement in plans/workers, including missing inventory and stale plans (Linux mocked package tests pass).
- [x] Select the PowerShell MSI during bootstrap, probe runtime startup with retained diagnostics, and bind the launcher to the validated executable (Linux mocked bootstrap/output tests pass).
- [ ] Verify MSI setup, broken .NET-tool/PATH recovery, and separate PowerShell updates on disposable Windows.

View File

@ -51,20 +51,38 @@ function Test-Administrator {
function Invoke-BootstrapWingetInstall {
param([Parameter(Mandatory = $true)][string]$Id)
& winget.exe install --id $Id --exact --source winget --silent --disable-interactivity --accept-package-agreements --accept-source-agreements --scope machine
$installerArguments = @('install', '--id', $Id, '--exact', '--source', 'winget', '--silent', '--disable-interactivity', '--accept-package-agreements', '--accept-source-agreements', '--scope', 'machine')
# Require the self-contained MSI instead of following WinGet's MSIX default.
if ($Id -eq 'Microsoft.PowerShell') { $installerArguments += @('--installer-type', 'wix') }
& winget.exe @installerArguments
if ($LASTEXITCODE -ne 0) { throw "WinGet failed to install '$Id' (exit $LASTEXITCODE)." }
}
function Find-PowerShell7Path {
$command = Get-Command pwsh.exe -ErrorAction SilentlyContinue
if ($command) { return $command.Source }
if (-not [string]::IsNullOrWhiteSpace($env:ProgramFiles)) {
$installedPath = Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe'
if (Test-Path -LiteralPath $installedPath -PathType Leaf) { return $installedPath }
}
$command = Get-Command pwsh.exe -CommandType Application -ErrorAction SilentlyContinue
if ($command) { return $command.Source }
return $null
}
function Assert-BootstrapPowerShell {
param([Parameter(Mandatory = $true)][string]$Path)
# Preserve native startup diagnostics (including missing .NET) under PS 5.1.
$previousPreference = $ErrorActionPreference
try {
$ErrorActionPreference = 'Continue'
$diagnostics = & $Path -NoLogo -NoProfile -NonInteractive -Command 'if ($PSVersionTable.PSVersion.Major -lt 7) { exit 1 }; Write-Output "ConfigCentral.PowerShell7.Ready"; exit 0' 2>&1
$code = $LASTEXITCODE
}
finally { $ErrorActionPreference = $previousPreference }
if ($code -ne 0 -or @($diagnostics) -notcontains 'ConfigCentral.PowerShell7.Ready') {
throw "PowerShell at '$Path' could not start (exit $code): $($diagnostics -join [Environment]::NewLine). Repair that installation or install the official PowerShell 7 MSI, then rerun setup."
}
}
function Find-GitPath {
$command = Get-Command git.exe -ErrorAction SilentlyContinue
if ($command) { return $command.Source }
@ -143,6 +161,7 @@ $git = Find-GitPath
if (-not $git) { throw 'Git was installed but git.exe could not be located.' }
$powerShell7 = Find-PowerShell7Path
if (-not $powerShell7) { throw 'PowerShell 7 was installed but pwsh.exe could not be located.' }
Assert-BootstrapPowerShell -Path $powerShell7
if (Test-Path -LiteralPath (Join-Path $Checkout '.git')) {
Push-Location $Checkout
try {
@ -185,7 +204,7 @@ $settings | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Join-Path $dataR
$launcherDirectory = Join-Path $env:LOCALAPPDATA 'ConfigCentral\bin'
New-Item -ItemType Directory -Path $launcherDirectory -Force | Out-Null
$launcherPath = Join-Path $launcherDirectory 'cc.cmd'
$launcherContent = "@echo off`r`npwsh.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File `"$Checkout\cc.ps1`" %*`r`n"
$launcherContent = "@echo off`r`n`"$powerShell7`" -NoLogo -NoProfile -ExecutionPolicy Bypass -File `"$Checkout\cc.ps1`" %*`r`n"
[System.IO.File]::WriteAllText($launcherPath, $launcherContent, [Text.Encoding]::ASCII)
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
if (($userPath -split ';') -notcontains $launcherDirectory) {

View File

@ -42,3 +42,48 @@ Describe 'Bootstrap invocation' {
finally { $env:ProgramFiles = $previousProgramFiles }
}
}
Describe 'Bootstrap PowerShell prerequisites' {
BeforeAll {
foreach ($name in @('Find-PowerShell7Path', 'Invoke-BootstrapWingetInstall', 'Assert-BootstrapPowerShell')) {
$fn = $bootstrapAst.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq $name }, $true)
. ([scriptblock]::Create($fn.Extent.Text))
}
function winget.exe { $global:LASTEXITCODE = 0 }
function Test-Runtime { $global:LASTEXITCODE = 0; 'ConfigCentral.PowerShell7.Ready' }
}
It 'requests the MSI only for PowerShell' {
Mock winget.exe { $global:LASTEXITCODE = 0 }
Invoke-BootstrapWingetInstall -Id Microsoft.PowerShell
Should -Invoke winget.exe -Times 1 -Exactly -ParameterFilter { $args -contains '--installer-type' -and $args -contains 'wix' -and $args -contains 'machine' }
Invoke-BootstrapWingetInstall -Id Git.Git
Should -Invoke winget.exe -Times 1 -Exactly -ParameterFilter { $args -contains 'Git.Git' -and $args -notcontains '--installer-type' }
}
It 'prefers the standard installation over a dotnet shim on PATH' {
$previousProgramFiles = $env:ProgramFiles
try {
$env:ProgramFiles = $TestDrive
$expected = Join-Path $TestDrive 'PowerShell/7/pwsh.exe'
New-Item -ItemType File -Path $expected -Force | Out-Null
Mock Get-Command { [pscustomobject]@{ Source = 'broken-dotnet-shim' } } -ParameterFilter { $Name -eq 'pwsh.exe' }
Find-PowerShell7Path | Should -Be $expected
}
finally { $env:ProgramFiles = $previousProgramFiles }
}
It 'accepts a working runtime' {
{ Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Not -Throw
}
It 'retains startup diagnostics and recommends repair' {
Mock Test-Runtime { $global:LASTEXITCODE = 1; 'You must install or update .NET' }
{ Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Throw '*install or update .NET*Repair*'
}
It 'rejects a shim that exits successfully without starting PowerShell' {
Mock Test-Runtime { $global:LASTEXITCODE = 0 }
{ Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Throw '*could not start*'
}
}