private/Save-WinPEDriverPackageWindowsLofWifi.ps1

#Requires -PSEdition Core

function Save-WinPEDriverPackageWindowsLofWifi {
    <#
    .SYNOPSIS
        Downloads a Windows Languages and Optional Features ISO and extracts Wi-Fi drivers
 
    .DESCRIPTION
        Downloads a Windows Languages and Optional Features ISO to the OSDeployCore download
        cache. Unless DownloadOnly is specified, the function mounts the ISO and expands each
        Microsoft-Windows-Wifi-Client CAB from LanguagesAndOptionalFeatures into a separate
        managed driver library subfolder. Subfolder names omit the CAB prefix and amd64 FOD suffix.
 
        Existing expanded files suppress extraction unless Force is specified. Mount, discovery,
        and expansion failures are written as warnings, and a mounted ISO is dismounted in a
        finally block.
 
    .PARAMETER DriverPackage
        Specifies the amd64 OSDeployWinPEDriver.Package object to download. The object supplies
        the package name, package ID, architecture, download URI, and filename.
 
    .PARAMETER Force
        Downloads the ISO again and attempts expansion even when the managed driver folder already
        contains driver files. Existing expanded content is not removed before extraction.
 
    .PARAMETER DownloadOnly
        Returns after downloading the ISO without mounting it or expanding CAB files.
 
    .EXAMPLE
        PS> $package = Get-WinPEDriverPackageCore | Where-Object Name -Match 'Windows.*WiFi' | Select-Object -First 1; Save-WinPEDriverPackageWindowsLofWifi -DriverPackage $package
 
        Downloads the first matching LOF package and extracts its Wi-Fi Client CAB files.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.IO.DirectoryInfo. Returns a destination directory for each successfully expanded CAB.
        System.IO.FileInfo. Returns the downloaded ISO when processing completes or expansion is skipped.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 1.0.0
        Date: 2026-08-28
 
        Requires Windows disk-image cmdlets and access to mount and dismount ISO files.
 
    .LINK
        Save-WinPEDriverPackageCore
    #>

    [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:OSDeployBootAssetsPath "winpedrivers-$($package.Architecture)" |
                   Join-Path -ChildPath "$($package.Name)-$($package.PackageId)"

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

    if (-not $downloadedFile) {
        return
    }

    if ($DownloadOnly) {
        return $downloadedFile
    }

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

    $diskImage = $null
    try {
        Write-Verbose "[$($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-Wifi-Client-*.cab' -ErrorAction Stop

        if (-not $cabs) {
            Write-Warning "[$(Get-Date -Format s)] No Microsoft-Windows-Wifi-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-Wifi-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-HostDateTimeDarkGray -Message "Expanding '$($cab.Name)' to '$destFolder'"
            Write-Verbose "[$($MyInvocation.MyCommand.Name)] Expanding '$($cab.FullName)' to '$destFolder'"
            Expand-WinPEDriverPackageCab -Path $cab.FullName -DestinationPath $destFolder
        }
    }
    catch {
        Write-Warning "[$(Get-Date -Format s)] $($_.Exception.Message)"
    }
    finally {
        if ($diskImage) {
            Dismount-DiskImage -ImagePath $targetPath -ErrorAction SilentlyContinue | Out-Null
            Write-Verbose "[$($MyInvocation.MyCommand.Name)] Dismounted '$targetPath'"
        }
    }

    $downloadedFile
}