| 1 | @echo off |
| 2 | setlocal EnableExtensions DisableDelayedExpansion |
| 3 | rem Run in a fresh standard-user session after Windows Setup. Set |
| 4 | rem CAGE_QA_EXPECTED_VM_ID to the GUID read independently from Get-VM on the host. |
| 5 | rem In guest PowerShell, set $env:CAGE_QA_EXPECTED_VM_ID before running this file. |
| 6 | "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -Command ^ |
| 7 | "$ErrorActionPreference = 'Stop';" ^ |
| 8 | "$expected = [guid]::Empty; if (![guid]::TryParse($env:CAGE_QA_EXPECTED_VM_ID, [ref]$expected) -or $expected -eq [guid]::Empty) { throw 'Supply a host-verified VM GUID.' }" ^ |
| 9 | "$system = Get-CimInstance Win32_ComputerSystem;" ^ |
| 10 | "if ($system.Manufacturer -ne 'Microsoft Corporation' -or $system.Model -ne 'Virtual Machine') { throw 'Run this check inside the selected Hyper-V guest.' }" ^ |
| 11 | "$actual = Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters' -Name VirtualMachineId;" ^ |
| 12 | "if ([guid]$actual -ne $expected) { throw 'The guest differs from the host-verified VM ID.' }" ^ |
| 13 | "$identity = [Security.Principal.WindowsIdentity]::GetCurrent();" ^ |
| 14 | "$groups = & (Join-Path $env:SystemRoot 'System32\whoami.exe') /groups /fo csv /nh;" ^ |
| 15 | "if (!$groups -or $LASTEXITCODE -ne 0 -or $identity.IsSystem -or $groups -match '\bS-1-5-32-544\b') { throw 'Sign in as a standard guest user outside the Administrators group.' }" ^ |
| 16 | "$legacy = $PSVersionTable.PSVersion;" ^ |
| 17 | "if ($PSVersionTable.PSEdition -ne 'Desktop' -or $legacy.Major -ne 5 -or $legacy.Minor -ne 1) { throw 'Expected the system Windows PowerShell 5.1.' }" ^ |
| 18 | "$command = Get-Command pwsh; $expectedExe = Join-Path ([Environment]::GetFolderPath('ProgramFiles')) 'PowerShell\7\pwsh.exe';" ^ |
| 19 | "if ($command.CommandType -ne 'Application' -or $command.Path -ne $expectedExe) { throw 'The user PATH does not resolve pwsh to the installed shell.' }" ^ |
| 20 | "$probe = @(pwsh -NoLogo -NoProfile -NonInteractive -Command '$PSVersionTable.PSEdition; $PSVersionTable.PSVersion.ToString(); [Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture.ToString()');" ^ |
| 21 | "if ($LASTEXITCODE -ne 0 -or $probe.Count -ne 3 -or $probe[0] -ne 'Core' -or $probe[1] -notmatch '^7\.[0-9]+\.[0-9]+$' -or $probe[2] -ne 'X64') { throw 'Expected a working x64 PowerShell 7 release.' }" ^ |
| 22 | "[pscustomobject]@{ VMId = $expected; User = $identity.Name; WindowsPowerShell = $legacy.ToString(); PowerShell = $probe[1]; Architecture = $probe[2]; Executable = $command.Path } | ConvertTo-Json -Compress" |
| 23 | exit /b %errorlevel% |