October 10

Fixing Sysprep Errors While Building Windows Server Images

When building Windows Server 2019 and 2022 images in Azure, you might encounter Sysprep errors that prevent the process from completing successfully. These errors usually arise during the image generalization phase, and they can be tricky to resolve, as seen in the following error messages:

azure-arm.windows2022: 2024-10-05 21:34:00, Error SYSPRP MRTGeneralize:98 - ERROR: Failed ConnectServer
azure-arm.windows2022: 2024-10-05 21:34:02, Error SYSPRP BCD: BiUpdateEfiEntry failed c000000d
azure-arm.windows2022: 2024-10-05 21:34:02, Error SYSPRP BCD: BiExportBcdObjects failed c000000d
azure-arm.windows2022: 2024-10-05 21:34:02, Error SYSPRP BCD: BiExportStoreAlterationsToEfi failed c000000d
azure-arm.windows2022: 2024-10-05 21:34:02, Error SYSPRP BCD: Failed to export alterations to firmware. Status: c000000d

The previous error message was caught from sysprep logs, which I found using the following condition:

if ($imageState -eq 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') {
        break
} elseif ($imageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') {
        $setupActLog = Get-Content "$Env:Windir\System32\Sysprep\Panther\setupact.log" -Raw
        $setupErrLog = Get-Content "$Env:Windir\System32\Sysprep\Panther\setuperr.log" -Raw
        Write-Output "setupact.log:"
        Write-Output $setupActLog
        Write-Output "setuperr.log:"
        Write-Output $setupErrLog
        Write-Output "The unexpected state: $imageState"
        exit 1
}

Overall system state must be IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE for creating an image, if not, we need to check the SysPrep logs.

The Root Cause: Windows Store and Mini-Setup Delays

Sometimes, the Sysprep errors are caused by Windows Store automatic updates running in the background. The mini-setup phase of Sysprep, which generalizes the image, may experience significant delays or failures when Windows Store services update during this time. Microsoft Premier Support has acknowledged this as a potential issue, but they did not provide a definitive fix.

The solution involves disabling the Windows Store automatic updates and ensuring the related services are stopped.

Source: https://learn.microsoft.com/en-us/answers/questions/333299/windows-10-sysprep

The Solution:

First, you need to add a registry key to disable Windows Store automatic updates. Here’s a PowerShell script that handles this task:

# Disable Windows Store Automatic Updates
Write-Host "Adding Registry key to Disable Windows Store Automatic Updates"
$registryPath = "HKLM:\SOFTWARE\Policies\Microsoft\WindowsStore"

If (!(Test-Path $registryPath)) {
    Mkdir $registryPath -ErrorAction SilentlyContinue
    New-ItemProperty $registryPath -Name AutoDownload -Value 2
}
Else {
    Set-ItemProperty $registryPath -Name AutoDownload -Value 2
}

Next, you need to stop the Windows Store installer service, which could be interfering with the Sysprep process.

# Stop WindowsStore Installer Service and set to Disabled
Write-Host "Stopping InstallService"
Stop-Service InstallService

This ensures that the InstallService responsible for handling Windows Store updates is stopped and will not start automatically during the generalization process.

Final Thoughts

Once these steps are complete, Sysprep should be able to run without encountering BCD errors or other issues related to the Windows Store updates. By disabling Windows Store automatic updates and stopping the associated services, you reduce the likelihood of conflicts during Sysprep’s generalization phase.

This fix has helped resolve similar issues for other users, and it’s a valuable solution to try if you’re experiencing Sysprep failures in Windows Server 2019 or 2022. Make sure to share this with others facing the same challenge!