private/WinPEDrivers/CloudWinPEDriver/Save-CloudWinPEDriverOemHp.ps1

#Requires -PSEdition Core

function Save-CloudWinPEDriverOemHp {
    <#
    .SYNOPSIS
        Downloads and expands the HP WinPE driver pack.
 
    .DESCRIPTION
        Internal helper called by Save-CloudWinPEDriver. Downloads the HP SoftPaq .exe to
        $env:ProgramData\OSDeployCore\downloads\, verifies the SHA256 checksum,
        removes stale expanded directories, and runs the self-extractor silently.
 
    .PARAMETER DriverPackage
        A OSDeployWinPEDriver.Package object from Get-CloudWinPEDriver.
 
    .PARAMETER Force
        Re-download even if the file already exists and the hash matches.
 
    .OUTPUTS
        [System.IO.FileInfo] The downloaded .exe 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.Version)"

    # Download
    $downloadedFile = Invoke-CloudWinPEDriverDownload `
        -Uri             $package.DownloadUri `
        -DestinationPath $targetPath `
        -SearchUri       $package.UpdateUri `
        -ExpectedSHA256  $package.Checksums.SHA256 `
        -Force:$Force

    if ($DownloadOnly) {
        return $downloadedFile
    }

    # Expand via self-extractor (/s = silent, /e = extract, /f = folder)
    $expandedFiles = Get-ChildItem -Path $driverDir -Recurse -File -ErrorAction SilentlyContinue
    if (-not $expandedFiles) {
        if (-not (Test-Path -Path $driverDir)) {
            New-Item -ItemType Directory -Path $driverDir -Force | Out-Null
        }
        Write-OSDeployWinPEDriversProgress -Message "Expand: $targetPath"
        Write-OSDeployWinPEDriversProgress -Message "Output: $driverDir"
        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Running self-extractor '$($package.FileName)'"
        $proc = Start-Process -FilePath $targetPath `
            -ArgumentList "/s /e /f `"$driverDir`"" `
            -NoNewWindow -Wait -PassThru
        if ($proc.ExitCode -ne 0) {
            # Self-extractors often return non-zero even on success; verify by checking files
            $expandedAfter = Get-ChildItem -Path $driverDir -Recurse -File -ErrorAction SilentlyContinue
            if (-not $expandedAfter) {
                Write-Error "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Self-extractor exited $($proc.ExitCode) and no files found in '$driverDir'"
            }
            else {
                Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Self-extractor exited $($proc.ExitCode) but files found. Continuing."
            }
        }
    }
    else {
        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] '$driverDir' already contains expanded files. Skipping expansion."
    }

    $downloadedFile
}