private/BootMedia/Steps/Step-BootImageExport.ps1

#Requires -PSEdition Core

function Step-BootImageExport {
    <#
    .SYNOPSIS
        Exports and optimizes the final boot.wim, then copies to MediaEX if applicable.
 
    .NOTES
        Author: David Segura
        Version: 0.1.0
    #>

    [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-OSDeployCoreProgress '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
    }
}