private/Step-BuildBootCopyOSDCloudModule.ps1

#Requires -PSEdition Core

function Step-BuildBootCopyOSDCloudModule {
    <#
    .SYNOPSIS
        Copies the installed OSDCloud module into the mounted WinPE image
 
    .DESCRIPTION
        Resolves the installed OSDCloud module path and version, creates its versioned
        destination under the mounted image, and copies the module with robocopy.exe.
        The copy log is appended in the build log directory.
 
        The function skips the copy with a warning when Get-OSDCloudModulePath is
        unavailable or the resolved source does not exist. After a successful manifest
        check, it appends the installed OSDCloud version to global
        BuildMedia.InstalledApps.
 
    .EXAMPLE
        PS> Step-BuildBootCopyOSDCloudModule
 
        Copies the available OSDCloud module into the mounted WinPE module path.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        None. robocopy.exe output is redirected to a log file and suppressed.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 0.1.0
        Date: 2026-08-28
 
        Requires global BuildMedia with MountPath, LogsPath, and InstalledApps
        properties, the OSDCloud commands Get-OSDCloudModulePath and
        Get-OSDCloudModuleVersion, and robocopy.exe. Modifies the mounted image and
        global BuildMedia.InstalledApps.
    #>

    [CmdletBinding()]
    param ()

    $MountPath = $global:BuildMedia.MountPath
    $LogsPath  = $global:BuildMedia.LogsPath

    # Require Get-OSDCloudModulePath to be available
    if (-not (Get-Command -Name 'Get-OSDCloudModulePath' -ErrorAction SilentlyContinue)) {
        Write-Warning "[$(Get-Date -Format s)] Get-OSDCloudModulePath is not available. Import the OSDCloud module before building."
        return
    }

    $ModuleSource = Get-OSDCloudModulePath

    if (-not $ModuleSource -or -not (Test-Path $ModuleSource)) {
        Write-Warning "[$(Get-Date -Format s)] OSDCloud module path not found: $ModuleSource"
        return
    }

    $ModuleVersion     = (Get-OSDCloudModuleVersion).ToString()
    $ModuleDestination = "$MountPath\Program Files\WindowsPowerShell\Modules\OSDCloud\$ModuleVersion"

    Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] Copying OSDCloud module v$ModuleVersion from $ModuleSource"

    if (-not (Test-Path $ModuleDestination)) {
        New-Item -Path $ModuleDestination -ItemType Directory -Force | Out-Null
    }

    $CurrentLog = "$LogsPath\$((Get-Date).ToString('yyMMdd-HHmmss'))-Copy-OSDCloudModule.log"
    $null = robocopy.exe "$ModuleSource" "$ModuleDestination" *.* /e /b /ndl /nfl /np /ts /r:0 /w:0 /xj /mt:128 /XD .git .github .vscode /LOG+:"$CurrentLog"

    if (Test-Path "$ModuleDestination\OSDCloud.psd1") {
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] OSDCloud module v$ModuleVersion injected into $ModuleDestination"
        $global:BuildMedia.InstalledApps += "OSDCloud $ModuleVersion"
    }
    else {
        Write-Warning "[$(Get-Date -Format s)] OSDCloud module copy may have failed. Expected manifest at $ModuleDestination\OSDCloud.psd1"
    }
}