private/WinPEDrivers/CoreWinPEDriver/Expand-CoreWinPEDriverIso.ps1

#Requires -PSEdition Core

function Expand-CoreWinPEDriverIso {
    <#
    .SYNOPSIS
        Mounts an ISO file and copies its contents to a destination directory.
 
    .DESCRIPTION
        Internal helper that mounts an ISO image with Mount-DiskImage, copies
        all contents to the destination directory, then dismounts the image.
        The ISO is always dismounted in a finally block even if an error occurs.
        Returns the destination directory on success.
 
    .PARAMETER Path
        Full path to the .iso file.
 
    .PARAMETER DestinationPath
        Directory where the ISO contents will be copied.
 
    .OUTPUTS
        [System.IO.DirectoryInfo] The extraction destination directory.
    #>

    [CmdletBinding()]
    [OutputType([System.IO.DirectoryInfo])]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$DestinationPath
    )

    if (-not (Test-Path -Path $Path)) {
        $PSCmdlet.ThrowTerminatingError(
            [System.Management.Automation.ErrorRecord]::new(
                [System.IO.FileNotFoundException]::new("ISO file not found: $Path"),
                'IsoFileNotFound',
                [System.Management.Automation.ErrorCategory]::ObjectNotFound,
                $Path
            )
        )
    }

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

    $diskImage = $null
    try {
        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Mounting ISO: $Path"
        $diskImage = Mount-DiskImage -ImagePath $Path -PassThru -ErrorAction Stop
        $driveLetter = ($diskImage | Get-Volume).DriveLetter
        if (-not $driveLetter) {
            throw "Could not determine drive letter for mounted ISO '$Path'."
        }

        $sourceRoot = "${driveLetter}:\"
        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Copying ISO contents from '$sourceRoot' to '$DestinationPath'"
        Copy-Item -Path "${sourceRoot}*" -Destination $DestinationPath -Recurse -Force

        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] ISO extraction complete: $DestinationPath"
        Get-Item -Path $DestinationPath
    }
    catch {
        $PSCmdlet.ThrowTerminatingError(
            [System.Management.Automation.ErrorRecord]::new(
                [System.InvalidOperationException]::new(
                    "ISO expansion failed for '$Path': $($_.Exception.Message)"
                ),
                'IsoExpandFailed',
                [System.Management.Automation.ErrorCategory]::InvalidResult,
                $Path
            )
        )
    }
    finally {
        if ($diskImage) {
            Dismount-DiskImage -ImagePath $Path -ErrorAction SilentlyContinue | Out-Null
            Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Dismounted ISO: $Path"
        }
    }
}