private/WinPEDrivers/CloudWinPEDriver/Save-CloudWinPEDriverOemVMwareAMD64.ps1

#Requires -PSEdition Core

function Save-CloudWinPEDriverOemVMwareAMD64 {
    <#
    .SYNOPSIS
        Downloads and expands the VMware Tools ISO for amd64.
 
    .DESCRIPTION
        Internal helper called by Save-CloudWinPEDriver. Downloads the VMware Tools Windows
        amd64 .iso to $env:ProgramData\OSDeployCore\downloads\, removes stale
        expanded directories, expands to a temporary directory, and copies only the
        pvscsi Win10 amd64 driver payload into the final package directory.
 
    .PARAMETER DriverPackage
        A OSDeployWinPEDriver.Package object from Get-CloudWinPEDriver with Architecture = 'amd64'.
 
    .PARAMETER Force
        Re-download even if the file already exists.
 
    .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.Version)"
    $relativeSourcePath = 'Program Files\VMware\VMware Tools\Drivers\pvscsi\Win10\amd64'
    $targetDriverDir = Join-Path $driverDir 'pvscsi'

    # VMware ISOs do not have published checksums — download without hash verification
    $downloadedFile = Invoke-CloudWinPEDriverDownload `
        -Uri             $package.DownloadUri `
        -DestinationPath $targetPath `
        -SearchUri       $package.UpdateUri `
        -Force:$Force

    if ($DownloadOnly) {
        return $downloadedFile
    }

    $targetFiles = Get-ChildItem -Path $targetDriverDir -Recurse -File -ErrorAction SilentlyContinue
    $extraContent = if (Test-Path -Path $driverDir -PathType Container) {
        Get-ChildItem -Path $driverDir -Force -ErrorAction SilentlyContinue |
            Where-Object { $_.Name -ne 'pvscsi' } |
            Select-Object -First 1
    }

    if ((-not $targetFiles) -or $extraContent) {
        $tempExpandDir = Join-Path ([System.IO.Path]::GetTempPath()) ("WinPEDrivers-VMwareAMD64-{0}" -f [guid]::NewGuid().Guid)

        try {
            if (Test-Path -Path $driverDir -PathType Container) {
                Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Resetting '$driverDir' before copying VMware pvscsi drivers."
                Get-ChildItem -Path $driverDir -Force -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force
            }
            else {
                New-Item -ItemType Directory -Path $driverDir -Force | Out-Null
            }

            New-Item -ItemType Directory -Path $tempExpandDir -Force | Out-Null
            Write-OSDeployWinPEDriversProgress -Message "Expanding '$targetPath' to '$targetDriverDir'"
            Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Expanding '$($package.FileName)' to temporary directory '$tempExpandDir'"
            Expand-CoreWinPEDriverVMwareToolsIso -Path $targetPath -DestinationPath $tempExpandDir | Out-Null

            $sourceDriverDir = Join-Path $tempExpandDir $relativeSourcePath
            if (-not (Test-Path -Path $sourceDriverDir -PathType Container)) {
                throw "Expected VMware pvscsi driver directory was not found: $sourceDriverDir"
            }

            New-Item -ItemType Directory -Path $targetDriverDir -Force | Out-Null
            Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Copying VMware pvscsi drivers from '$sourceDriverDir' to '$targetDriverDir'"
            Copy-Item -Path (Join-Path $sourceDriverDir '*') -Destination $targetDriverDir -Recurse -Force
        }
        finally {
            if (Test-Path -Path $tempExpandDir -PathType Container) {
                Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Removing temporary directory '$tempExpandDir'"
                Remove-Item -Path $tempExpandDir -Recurse -Force
            }
        }
    }
    else {
        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] '$targetDriverDir' already contains VMware pvscsi drivers. Skipping expansion."
    }

    $downloadedFile
}