Monday, July 6, 2015

PowerShell Tip : Extract contents of an MSI

Every once in a while I need to extract the content of a .msi file in order to customize a deployment for a particular network environment. Using the MSIexec I can specify the administrative options to extract the package contents to a target folder. For a better management, I've created a PowerShell function to do the same.



Function Export-MsiContents
{
       [CmdletBinding()]
       param
       (
              [Parameter(Mandatory = $true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [ValidateScript({Test-Path $_})]
        [ValidateScript({$_.EndsWith(".msi")})]
        [String] $MsiPath,

        [Parameter(Mandatory=$false, Position=1)]
        [String] $TargetDirectory
       )

    if(-not($TargetDirectory))
    {
        $currentDir = [System.IO.Path]::GetDirectoryName($MsiPath)
        Write-Warning "A target directory is not specified. The contents of the MSI will be extracted to the location, $currentDir\Temp"
        $TargetDirectory = Join-Path $currentDir "Temp"
    }

    $MsiPath = Resolve-Path $MsiPath

    Write-Verbose "Extracting the contents of $MsiPath to $TargetDirectory"
    Start-Process "MSIEXEC" -ArgumentList "/a $MsiPath /qn TARGETDIR=$TargetDirectory" -Wait -NoNewWindow
}

PS C:\> Export-MsiContents -MsiPath C:\Binaries\Setup.msi -Verbose
WARNING: A target directory is not specified. The contents of the MSI will be extracted to the location, C:\Binaries\Temp
VERBOSE: Extracting the contents of C:\Binaries\Setup.msi to C:\Binaries\Temp



No comments: