private/WinPEDrivers/CloudWinPEDriver/Save-CloudWinPEDriverWindowsEthernet.ps1

#Requires -PSEdition Core

function Save-CloudWinPEDriverWindowsEthernet {
    <#
    .SYNOPSIS
        Downloads the Windows LOF ISO and expands Ethernet Client cab files.
 
    .DESCRIPTION
        Internal helper called by Save-CloudWinPEDriver. Downloads the Windows LOF ISO to
        $env:ProgramData\OSDeployCore\cache\downloads\, mounts it, and searches
        LanguagesAndOptionalFeatures for Microsoft-Windows-Ethernet-Client-*.cab files.
        Each cab is renamed by stripping the 'Microsoft-Windows-Ethernet-Client-' prefix
        and '-FOD-Package~31bf3856ad364e35~amd64~~' suffix, then expanded into its own
        subfolder under the driver package directory.
 
    .PARAMETER DriverPackage
        A OSDeployWinPEDriver.Package object from Get-CloudWinPEDriver with Architecture = 'amd64'.
 
    .PARAMETER Force
        Re-download even if the file already exists, and re-expand even if driver files
        are already present.
 
    .OUTPUTS
        [System.IO.FileInfo] The downloaded .iso file.
    #>

    [CmdletBinding()]
    [OutputType([System.IO.FileInfo])]
    param (
        [Parameter(Mandatory)]
        [PSTypeName('OSDeployWinPEDriver.Package')]
        [PSCustomObject]$DriverPackage,

        [Parameter()]
        [switch]$Force,

        [Parameter()]
        [switch]$DownloadOnly
    )

    $package     = $DriverPackage
    $idPrefix    = ($package.Name -split '-')[0]
    $downloadDir = Join-Path $script:OSDeployCoreDownloadsPath $idPrefix
    $targetPath  = Join-Path $downloadDir $package.FileName
    $driverDir   = Join-Path $script:OSDeployCoreRepositoryPath 'winpe-drivers' |
                   Join-Path -ChildPath $package.Architecture |
                   Join-Path -ChildPath "$($package.Name)-$($package.PackageId)"

    $downloadedFile = Invoke-CloudWinPEDriverDownload `
        -Uri             $package.DownloadUri `
        -DestinationPath $targetPath `
        -Force:$Force

    if ($DownloadOnly) {
        return $downloadedFile
    }

    $expandedFiles = Get-ChildItem -Path $driverDir -Recurse -File -ErrorAction SilentlyContinue
    if ($expandedFiles -and -not $Force) {
        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] '$driverDir' already contains expanded files. Skipping expansion."
        return $downloadedFile
    }

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

        $lofPath = "${driveLetter}:\LanguagesAndOptionalFeatures"
        $cabs    = Get-ChildItem -Path $lofPath -Filter 'Microsoft-Windows-Ethernet-Client-*.cab' -ErrorAction Stop

        if (-not $cabs) {
            Write-Warning "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] No Microsoft-Windows-Ethernet-Client-*.cab files found in '$lofPath'."
            return $downloadedFile
        }

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

        foreach ($cab in $cabs) {
            $folderName = $cab.BaseName `
                -replace '^Microsoft-Windows-Ethernet-Client-', '' `
                -replace '-FOD-Package~31bf3856ad364e35~amd64~~$', ''

            $destFolder = Join-Path $driverDir $folderName

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

            Write-OSDeployWinPEDriversProgress -Message "Expanding '$($cab.Name)' to '$destFolder'"
            Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Expanding '$($cab.FullName)' to '$destFolder'"
            Expand-CoreWinPEDriverCab -Path $cab.FullName -DestinationPath $destFolder
        }
    }
    catch {
        Write-Warning "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] $($_.Exception.Message)"
    }
    finally {
        if ($diskImage) {
            Dismount-DiskImage -ImagePath $targetPath -ErrorAction SilentlyContinue | Out-Null
            Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Dismounted '$targetPath'"
        }
    }

    $downloadedFile
}