Tuesday 15 May 2018

PowerShell: Script to display the Properties in MSI(Windows Installer)

For editing MSI we need to have a tool like ORCA, Install shield, Wise Package studio, etc.
By using the PowerShell script, we can get the properties from MSI without any tool.

Script:

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

    $MsiPath = Resolve-Path $MsiPath
    $type = [Type]::GetTypeFromProgID("WindowsInstaller.Installer")
    $installer = [Activator]::CreateInstance($type)

    $db = Invoke-MemberOnType "OpenDatabase" $installer @($MsiPath,0)
  
    $view = Invoke-MemberOnType "OpenView" $db ('SELECT * FROM Property')

    Invoke-MemberOnType "Execute" $view $null

    $record = Invoke-MemberOnType "Fetch" $view $null
    while($record -ne $null)
    {
        $property = Invoke-MemberOnType "StringData" $record 1 "GetProperty"
        $value = Invoke-MemberOnType "StringData" $record 2 "GetProperty"
        Write-Output "$property = $value"
        $record = Invoke-MemberOnType "Fetch" $view $null
    }

    Invoke-MemberOnType "Close" $view $null
}


Function Invoke-MemberOnType
{
    param
    (
        [Parameter(Mandatory=$true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [String]$Name,
      
        [Parameter(Mandatory=$false, Position = 3)]
        [System.Reflection.BindingFlags]$InvokeAttr = "InvokeMethod",

        [Parameter(Mandatory=$true, Position = 1)]
        [ValidateNotNull()]
        [Object]$Target,
       
        [Parameter(Mandatory=$false, Position = 2)]
        [Object[]]$Arguments = $null
    )

    $Target.GetType().InvokeMember($Name,$InvokeAttr,$null,$Target,$Arguments)
}

Get-MsiProperties -MsiPath "C:\subinacl.msi" | Out-GridView


Steps To Do:
1. Copy the code and save the script as XXX.PS1
2. Set the execution policy to run the script.
3. Execute the script and it will display the properties detail.


Note: Above script, has taken reference from the Microsoft Tech Net site.

No comments:

Post a Comment