| 1 | @echo off |
| 2 | setlocal EnableExtensions DisableDelayedExpansion |
| 3 | rem Windows Setup runs this command from Cage's read-only answer DVD as SYSTEM. |
| 4 | set "CAGE_PWSH_MEDIA=%~d0\" |
| 5 | set "CAGE_PWSH_HASH=__CAGE_PWSH_SHA256__" |
| 6 | set "CAGE_PWSH_VERSION=__CAGE_PWSH_VERSION__" |
| 7 | "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -Command ^ |
| 8 | "$ErrorActionPreference = 'Stop';" ^ |
| 9 | "$medium = [IO.DriveInfo]::new($env:CAGE_PWSH_MEDIA);" ^ |
| 10 | "if ($medium.DriveType -ne 'CDRom' -or $medium.VolumeLabel -ne 'CAGE_ANSWER' -or ![Environment]::Is64BitProcess -or ![Security.Principal.WindowsIdentity]::GetCurrent().IsSystem) { throw 'PowerShell guest installation requires Windows Setup and the Cage answer DVD.' }" ^ |
| 11 | "$system = Get-CimInstance Win32_ComputerSystem;" ^ |
| 12 | "if ($system.Manufacturer -ne 'Microsoft Corporation' -or $system.Model -ne 'Virtual Machine') { throw 'Expected the Hyper-V guest.' }" ^ |
| 13 | "$zip = Join-Path $medium.RootDirectory.FullName 'PowerShell.zip';" ^ |
| 14 | "$base = Join-Path ([Environment]::GetFolderPath('ProgramFiles')) 'PowerShell';" ^ |
| 15 | "$target = Join-Path $base '7';" ^ |
| 16 | "function Test-Runtime([string]$directory, [string]$expectedVersion) {" ^ |
| 17 | " $exe = Join-Path $directory 'pwsh.exe';" ^ |
| 18 | " $probe = @(& $exe -NoLogo -NoProfile -NonInteractive -Command '$PSVersionTable.PSVersion.ToString(); [Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture.ToString()');" ^ |
| 19 | " if ($LASTEXITCODE -ne 0 -or $probe.Count -ne 2 -or $probe[0] -notmatch '^7\.[0-9]+\.[0-9]+$' -or $probe[1] -ne 'X64' -or ($expectedVersion -and $probe[0] -ne $expectedVersion)) { throw 'The installed shell did not report the expected x64 PowerShell 7 version.' }" ^ |
| 20 | "}" ^ |
| 21 | "if ((Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash -ne $env:CAGE_PWSH_HASH) { throw 'The offline PowerShell package does not match its pinned SHA-256.' }" ^ |
| 22 | "$null = [IO.Directory]::CreateDirectory($base);" ^ |
| 23 | "if ((Get-Item -LiteralPath $base).Attributes -band [IO.FileAttributes]::ReparsePoint) { throw 'The PowerShell installation folder is redirected.' }" ^ |
| 24 | "if (!(Test-Path -LiteralPath $target)) {" ^ |
| 25 | " $stage = Join-Path $base ([guid]::NewGuid().ToString('N'));" ^ |
| 26 | " try {" ^ |
| 27 | " Add-Type -AssemblyName System.IO.Compression.FileSystem;" ^ |
| 28 | " [IO.Compression.ZipFile]::ExtractToDirectory($zip, $stage);" ^ |
| 29 | " Test-Runtime $stage $env:CAGE_PWSH_VERSION;" ^ |
| 30 | " try { [IO.Directory]::Move($stage, $target) } catch [IO.IOException] { if (![IO.Directory]::Exists($target)) { throw } }" ^ |
| 31 | " } finally { if ([IO.Directory]::Exists($stage)) { [IO.Directory]::Delete($stage, $true) } }" ^ |
| 32 | "}" ^ |
| 33 | "if ((Get-Item -LiteralPath $target).Attributes -band [IO.FileAttributes]::ReparsePoint) { throw 'The installed PowerShell directory is redirected.' }" ^ |
| 34 | "Test-Runtime $target '';" ^ |
| 35 | "$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', $true);" ^ |
| 36 | "try {" ^ |
| 37 | " $raw = [string]$key.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames);" ^ |
| 38 | " $entries = @($raw.Split(';') | ForEach-Object { [Environment]::ExpandEnvironmentVariables($_.Trim().Trim([char]34)).TrimEnd('\') });" ^ |
| 39 | " if ($target -notin $entries) {" ^ |
| 40 | " $newPath = if ($raw) { $target + ';' + $raw } else { $target };" ^ |
| 41 | " $kind = if ('Path' -in $key.GetValueNames()) { $key.GetValueKind('Path') } else { [Microsoft.Win32.RegistryValueKind]::ExpandString };" ^ |
| 42 | " $key.SetValue('Path', $newPath, $kind);" ^ |
| 43 | " }" ^ |
| 44 | "} finally { $key.Close() }" |
| 45 | exit /b %errorlevel% |