private/WinPEDrivers/CoreWinPEDriver/Expand-CoreWinPEDriverVMwareToolsIso.ps1

#Requires -PSEdition Core

function Expand-CoreWinPEDriverVMwareToolsIso {
    <#
    .SYNOPSIS
        Mounts a VMware Tools ISO and runs an administrative install to extract driver files.
 
    .DESCRIPTION
        Internal helper that mounts a VMware Tools ISO, runs setup.exe with the /a flag
        to perform an administrative install (MSI extraction) to the destination directory,
        then dismounts the image. The ISO is always dismounted in a finally block even if
        an error occurs. Returns the destination directory on success.
 
    .PARAMETER Path
        Full path to the VMware Tools .iso file.
 
    .PARAMETER DestinationPath
        Directory where the administrative install will extract files.
 
    .OUTPUTS
        [System.IO.DirectoryInfo] The extraction destination directory.
    #>

    [CmdletBinding()]
    [OutputType([System.IO.DirectoryInfo])]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$DestinationPath
    )

    if (-not (Test-Path -Path $Path)) {
        $PSCmdlet.ThrowTerminatingError(
            [System.Management.Automation.ErrorRecord]::new(
                [System.IO.FileNotFoundException]::new("ISO file not found: $Path"),
                'IsoFileNotFound',
                [System.Management.Automation.ErrorCategory]::ObjectNotFound,
                $Path
            )
        )
    }

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

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

        $setupExe = "${driveLetter}:\setup.exe"
        if (-not (Test-Path -Path $setupExe)) {
            throw "setup.exe not found on mounted ISO at '$setupExe'."
        }

        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Running administrative install: $setupExe /a `"$DestinationPath`" /s /v/qn"
        $process = Start-Process -FilePath $setupExe -ArgumentList "/a `"$DestinationPath`" /s /v/qn" -Wait -PassThru -NoNewWindow -ErrorAction Stop

        if ($process.ExitCode -ne 0) {
            throw "VMware Tools administrative install exited with code $($process.ExitCode)."
        }

        Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Administrative install completed to '$DestinationPath'"
        Get-Item -Path $DestinationPath
    }
    catch {
        $PSCmdlet.ThrowTerminatingError(
            [System.Management.Automation.ErrorRecord]::new(
                [System.InvalidOperationException]::new(
                    "VMware Tools ISO expansion failed for '$Path': $($_.Exception.Message)"
                ),
                'VmwareIsoExpandFailed',
                [System.Management.Automation.ErrorCategory]::InvalidResult,
                $Path
            )
        )
    }
    finally {
        if ($diskImage) {
            Dismount-DiskImage -ImagePath $Path -ErrorAction SilentlyContinue | Out-Null
            Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Dismounted ISO: $Path"
        }
    }
}