private/Step-BuildBootCoreMediaScript.ps1

#Requires -PSEdition Core

function Step-BuildBootCoreMediaScript {
    <#
    .SYNOPSIS
        Runs configured and profile-local media build scripts
 
    .DESCRIPTION
        Runs script paths from global BuildMedia.MediaScript, followed by PowerShell
        scripts discovered in the build profile's boot-mediascript directory and its
        immediate subdirectories. Paths are processed in configured-first order, and
        duplicate paths are run only once. Missing paths produce warnings.
 
        Each script runs in the caller's session and can modify build state and media
        files. Success-stream output from invoked scripts is passed through.
 
    .EXAMPLE
        PS> Step-BuildBootCoreMediaScript
 
        Runs the configured and profile-local media build scripts.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.Object. Passes through success-stream output produced by each invoked script.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 0.1.0
        Date: 2026-08-28
 
        Requires global BuildMedia with MediaScript and BuildProfile properties.
        BuildProfileContentPath optionally overrides the profile-local content directory.
    #>

    [CmdletBinding()]
    param ()

    # Start with scripts explicitly configured in osdeployboot.json.
    $BuildMediaScript = @($global:BuildMedia.MediaScript | Where-Object { $_ })
    $BuildProfilePath = $global:BuildMedia.BuildProfile
    $BuildProfileContentPath = $global:BuildMedia.BuildProfileContentPath
    if (-not $BuildProfileContentPath -and $BuildProfilePath) {
        $BuildProfileContentPath = Split-Path -Path $BuildProfilePath -Parent
    }
    $ProfileMediaScriptPath = if ($BuildProfileContentPath) {
        Join-Path $BuildProfileContentPath 'boot-mediascript'
    }

    # Append profile-local scripts from boot-mediascript and its immediate subdirectories.
    if ($ProfileMediaScriptPath -and (Test-Path -LiteralPath $ProfileMediaScriptPath -PathType Container)) {
        $BuildMediaScript += Get-ChildItem -LiteralPath $ProfileMediaScriptPath -Filter '*.ps1' -File -Recurse -Depth 1 -ErrorAction SilentlyContinue |
            Sort-Object FullName |
            Select-Object -ExpandProperty FullName
    }

    # Preserve execution order while preventing the same path from running more than once.
    $SeenScripts = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
    $BuildMediaScript = @($BuildMediaScript | Where-Object { $SeenScripts.Add([System.String]$_) })

    # Run each available script and warn when a configured path no longer exists.
    foreach ($Item in $BuildMediaScript) {
        if (Test-Path -LiteralPath $Item -PathType Leaf) {
            Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] boot-mediascript: $Item"
            & "$Item"
        }
        else {
            Write-Warning "[$(Get-Date -Format s)] BootMedia Script not found: $Item"
        }
    }
}