$ErrorActionPreference = 'Stop'
$release = '__IMAGE_VERSION__'
$hotfixes = @(Get-HotFix -ErrorAction SilentlyContinue | Sort-Object InstalledOn -Descending)
$latest = $hotfixes | Select-Object -First 1
$manifest = [ordered]@{
    ImageVersion = $release
    OsCaption = (Get-CimInstance Win32_OperatingSystem).Caption
    OsBuild = (Get-CimInstance Win32_OperatingSystem).BuildNumber
    IisInstalled = [bool](Get-WindowsFeature Web-Server).Installed
    PatchInventoryCount = $hotfixes.Count
    LatestPatchId = if ($latest) { [string]$latest.HotFixID } else { 'BaseImageCurrent' }
    BaselineRevision = [int](Get-ItemProperty 'HKLM:\SOFTWARE\Workshopz\GoldenImage').BaselineRevision
}
$manifest | ConvertTo-Json -Depth 5 | Set-Content -Path 'C:\ProgramData\Workshopz\build-manifest.json' -Encoding utf8
Start-Service -Name W3SVC
$response = Invoke-WebRequest -Uri 'http://localhost/' -UseBasicParsing -TimeoutSec 20
if ($response.StatusCode -ne 200) { throw 'The local IIS health check failed.' }
