private/Step-BuildBootExportWindowsImage.ps1

#Requires -PSEdition Core

function Step-BuildBootExportWindowsImage {
    <#
    .SYNOPSIS
        Exports and replaces the final boot image
 
    .DESCRIPTION
        Exports index 1 of SourcesPath\boot.wim to a temporary export.wim, removes the
        original boot.wim, and renames the export to boot.wim. Existing export.wim is
        removed first. A timestamped Export-WindowsImage log is written.
 
        The function exports image metadata as CLIXML and JSON under CorePath. When the
        parent of SourcesPathEX exists, it also replaces that media tree's boot.wim.
 
    .EXAMPLE
        PS> Step-BuildBootExportWindowsImage
 
        Exports index 1, replaces boot.wim, writes metadata, and updates extended media.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        None.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 0.1.0
        Date: 2026-08-28
 
        Requires global BuildMedia with SourcesPath, SourcesPathEX, CorePath, and
        LogsPath properties and the Windows image servicing cmdlets. Destructively
        replaces boot.wim and any existing export.wim in SourcesPath.
    #>

    [CmdletBinding()]
    param ()

    $SourcesPath = $global:BuildMedia.SourcesPath
    $SourcesPathEX = $global:BuildMedia.SourcesPathEX
    $CorePath = $global:BuildMedia.CorePath
    $LogsPath = $global:BuildMedia.LogsPath

    $bootWimPath = Join-Path $SourcesPath 'boot.wim'
    $exportWimPath = Join-Path $SourcesPath 'export.wim'

    Write-HostDateTimeDarkGray 'Export-WindowsImage'

    if (Test-Path $exportWimPath) {
        Remove-Item -Path $exportWimPath -Force -ErrorAction Stop | Out-Null
    }

    $CurrentLog = "$LogsPath\$((Get-Date).ToString('yyMMdd-HHmmss'))-Export-WindowsImage.log"
    Export-WindowsImage -SourceImagePath $bootWimPath -SourceIndex 1 -DestinationImagePath $exportWimPath -LogPath $CurrentLog | Out-Null

    Remove-Item -Path $bootWimPath -Force -ErrorAction Stop | Out-Null
    Rename-Item -Path $exportWimPath -NewName 'boot.wim' -Force -ErrorAction Stop | Out-Null

    # Export metadata
    Get-WindowsImage -ImagePath $bootWimPath -Index 1 | Export-Clixml -Path "$CorePath\winpe-windowsimage.xml"
    Get-WindowsImage -ImagePath $bootWimPath -Index 1 | ConvertTo-Json -Depth 5 | Out-File "$CorePath\winpe-windowsimage.json" -Encoding utf8 -Force

    # Copy to MediaEX if present
    if ($SourcesPathEX -and (Test-Path (Split-Path $SourcesPathEX -Parent))) {
        Copy-Item -Path $bootWimPath -Destination (Join-Path $SourcesPathEX 'boot.wim') -Force -ErrorAction Stop | Out-Null
    }
}