private/WinPEDrivers/CloudWinPEDriver/Save-CloudWinPEDriverOemVMwareARM64.ps1

#Requires -PSEdition Core

function Save-CloudWinPEDriverOemVMwareARM64 {
    <#
    .SYNOPSIS
        Downloads and expands the VMware Tools ISO for arm64.
 
    .DESCRIPTION
        Internal helper called by Save-CloudWinPEDriver. Downloads the VMware Tools Windows
        arm64 .iso to $env:ProgramData\OSDeployCore\downloads\, removes stale
        expanded directories, expands to a temporary directory, and copies only the
        arm64 driver payload directories into the final package directory.
 
    .PARAMETER DriverPackage
        A OSDeployWinPEDriver.Package object from Get-CloudWinPEDriver with Architecture = 'arm64'.
 
    .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)"
    $allowedDriverDirectories = @('svga', 'vmusbmouse', 'vmxnet3')

    # 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
    }

    $targetDirectories = @(
        foreach ($directoryName in $allowedDriverDirectories) {
            Join-Path $driverDir $directoryName
        }
    )
    $targetFiles = Get-ChildItem -Path $targetDirectories -Recurse -File -ErrorAction SilentlyContinue
    $extraContent = if (Test-Path -Path $driverDir -PathType Container) {
        Get-ChildItem -Path $driverDir -Force -ErrorAction SilentlyContinue |
            Where-Object { $_.Name -notin $allowedDriverDirectories } |
            Select-Object -First 1
    }

    if ((-not $targetFiles) -or $extraContent) {
        $tempExpandDir = Join-Path ([System.IO.Path]::GetTempPath()) ("WinPEDrivers-VMwareARM64-{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 arm64 driver directories."
                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 "Expand: $targetPath"
        Write-OSDeployWinPEDriversProgress -Message "Output: $driverDir"
            Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Expanding '$($package.FileName)' to temporary directory '$tempExpandDir'"
            Expand-CoreWinPEDriverIso -Path $targetPath -DestinationPath $tempExpandDir | Out-Null

            $sourceDirectories = @(
                foreach ($directoryName in $allowedDriverDirectories) {
                    $sourceDirectory = Join-Path $tempExpandDir $directoryName
                    if (Test-Path -Path $sourceDirectory -PathType Container) {
                        Get-Item -Path $sourceDirectory
                    }
                }
            )

            if (-not $sourceDirectories) {
                throw "Expected VMware arm64 driver directories were not found in '$tempExpandDir'."
            }

            foreach ($sourceDirectory in $sourceDirectories) {
                $destinationDirectory = Join-Path $driverDir $sourceDirectory.Name
                Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Copying VMware arm64 driver directory '$($sourceDirectory.FullName)' to '$destinationDirectory'"
                Copy-Item -Path $sourceDirectory.FullName -Destination $driverDir -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)] '$driverDir' already contains VMware arm64 driver directories. Skipping expansion."
    }

    $downloadedFile
}