Monday, July 6, 2015

PowerShell Tip : Read MSI details

Here is a function, that you can use to read the properties of an .msi file.



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

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

    #The OpenDatabase method of the Installer object opens an existing database or creates a new one, returning a Database object
    #For our case, we need to open the database in read only. The open mode is 0
    $db = Invoke-MemberOnType "OpenDatabase" $installer @($MsiPath,0)
   
    #The OpenView method of the Database object returns a View object that represents the query specified by a SQL string.
    $view = Invoke-MemberOnType "OpenView" $db ('SELECT * FROM Property')

    #The Execute method of the View object uses the question mark token to represent parameters in an SQL statement.
    Invoke-MemberOnType "Execute" $view $null

    #The Fetch method of the View object retrieves the next row of column data if more rows are available in the result set, otherwise it is 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 = $property : Value = $value"
        $record = Invoke-MemberOnType "Fetch" $view $null
    }

    #The Close method of the View object terminates query execution and releases database resources.
    Invoke-MemberOnType "Close" $view $null
}


Function Invoke-MemberOnType
{
    param
    (
        #The string containing the name of the method to invoke
        [Parameter(Mandatory=$true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [String]$Name,
       
        #A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
        #The access can be one of the BindingFlags such as Public, NonPublic, Private, InvokeMethod, GetField, and so on
        [Parameter(Mandatory=$false, Position = 3)]
        [System.Reflection.BindingFlags]$InvokeAttr = "InvokeMethod",

        #The object on which to invoke the specified member
        [Parameter(Mandatory=$true, Position = 1)]
        [ValidateNotNull()]
        [Object]$Target,

        #An array containing the arguments to pass to the member to invoke.
        [Parameter(Mandatory=$false, Position = 2)]
        [Object[]]$Arguments = $null
    )

    #Invokes the specified member, using the specified binding constraints and matching the specified argument list.

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

Usage: 
 
PS C:\> Get-MsiProperties -MsiPath 'C:\Visual Studio Team Foundation Server 2013 Update 2 Power Tools .msi'
Property = DiskPrompt : Value = [1]
Property = UpgradeCode : Value = {4478A3A8-709A-418E-AF07-C695ACB5E8E0}
Property = SetupType : Value = Typical
Property = _IsSetupTypeMin : Value = Typical
Property = AgreeToLicense : Value = No
Property = AGREETOEXECUTIONPOLICYCHANGE : Value = No
Property = _IsMaintenance : Value = Change
Property = Display_IsBitmapDlg : Value = 1
Property = RANUI : Value = 0
Property = ADMINORADVERTISEDMSG : Value = This installer package does not support Admin or Advertised installations.
Property = ProgressType0 : Value = install
Property = DialogCaption : Value = Setup Windows Installer
Property = ARPURLINFOABOUT : Value = http://www.microsoft.com
Property = InstallChoice : Value = AR
Property = ApplicationUsers : Value = AllUsers
Property = DWUSINTERVAL : Value = 30
Property = ProgressType3 : Value = installs
Property = TestDir : Value = c:\test
Property = ReinstallModeText : Value = omus
Property = DisplayNameCustom : Value = Custom
Property = ProgressType2 : Value = installed
Property = DisplayNameTypical : Value = Typical
Property = PIDTemplate : Value = 12345<###-%%%%%%%>@@@@@
Property = INSTALLLEVEL : Value = 100
Property = DisplayNameMinimal : Value = Minimal
Property = ProgressType1 : Value = Installing
Property = CA_DEVENV_RUNNING : Value = 0
Property = DefaultUIFont : Value = Tahoma8
Property = ErrorDialog : Value = SetupError
Property = Manufacturer : Value = Microsoft Corporation
Property = ProductCode : Value = {36AEC772-4D0E-4986-8A53-4761C971EEC5}
Property = ProductLanguage : Value = 1033
Property = ProductName : Value = Microsoft Visual Studio Team Foundation Server 2013 Power Tools - Update 2
Property = ProductVersion : Value = 12.1.00402.0
Property = SecureCustomProperties : Value = AGREETOEXECUTIONPOLICYCHANGE;BPASNAPINEXISTS;NETFRAMEWORK20;NETFRAMEWORK35;NETFRAMEWORK40;NEWAPPFOUND;OLDAPPFOUND;POWERSHELL3INSTALLED;POWERSHELLINST
ALLED;PSEXECUTIONPOLICY;STORYBOARDINGTOOL;TEAMEXPLORER;TFSINSTALLREG;VSDIRECTORY;VSINSTALLREG
Property = WixPdbPath : Value = S:\dd\pt_dev12_qu2\binaries\x86ret\bin\i386\PowerToys\tfpt.wixpdb


No comments: