private/WinPEDrivers/CoreWinPEDriver/Expand-CoreWinPEDriverMsi.ps1

#Requires -PSEdition Core

function Expand-CoreWinPEDriverMsi {
    <#
    .SYNOPSIS
        Extracts the contents of an MSI file using an administrative install.
 
    .DESCRIPTION
        Internal helper that runs msiexec.exe with /a to extract the contents
        of an MSI package to a destination directory. Returns the destination
        directory when extraction succeeds.
 
    .PARAMETER Path
        Full path to the .msi file.
 
    .PARAMETER DestinationPath
        Directory where the MSI contents will be extracted.
 
    .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 -PathType Leaf)) {
        $PSCmdlet.ThrowTerminatingError(
            [System.Management.Automation.ErrorRecord]::new(
                [System.IO.FileNotFoundException]::new("MSI file not found: $Path"),
                'MsiFileNotFound',
                [System.Management.Automation.ErrorCategory]::ObjectNotFound,
                $Path
            )
        )
    }

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

    Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Expanding MSI '$Path' to '$DestinationPath'"

    $process = Start-Process -FilePath 'msiexec.exe' `
        -ArgumentList @('/a', "`"$Path`"", "targetdir=`"$DestinationPath`"", '/qn') `
        -NoNewWindow -Wait -PassThru

    if ($process.ExitCode -ne 0) {
        $expandedFiles = Get-ChildItem -Path $DestinationPath -Recurse -File -ErrorAction SilentlyContinue
        if (-not $expandedFiles) {
            $PSCmdlet.ThrowTerminatingError(
                [System.Management.Automation.ErrorRecord]::new(
                    [System.InvalidOperationException]::new("MSI expansion failed for '$Path' with exit code $($process.ExitCode)."),
                    'MsiExpandFailed',
                    [System.Management.Automation.ErrorCategory]::InvalidResult,
                    $Path
                )
            )
        }
    }

    Get-Item -Path $DestinationPath
}