Thursday, April 30, 2015

Installing a windows MSI on the commandline, detecting MSI parameters (aka properties)

Installing a MSI is fairly straightforward. Here's an example of installing python without any gui prompting to a designated directory.
msiexec /i "python-2.7.9.amd64.msi" /passive TARGETDIR="C:\tools\python2"
But how do I know about the TARGETDIR option I hear you say? Amazingly the msiexec binary can't tell you what parameters (public external properties) the MSI will accept. TARGETDIR appears to be a standard installshield property, but the MSI can expose others. The only way I found that doesn't require additional tools was to enumerate the parameters by dropping the MSI onto this piece of VBScript:
Call GetArguments(ArgArray) 


If IsArray(ArgArray) then

For Each ArrayElement In ArgArray
Wscript.Echo GetMSIProperties(ArrayElement)
Next

Else

WScript.Echo "Drag and drop MSI-File over the Script"

End if


' ----------------------------------------
Private Function GetMSIProperties(strMSIFile)

Dim oWI : Set oWI = CreateObject("WindowsInstaller.Installer")
Dim oDB : Set oDB = oWI.OpenDatabase(strMSIFile, 2)
Dim oView : Set oView = oDB.OpenView("Select * From Property")
Dim oRecord
oView.Execute

Do
Set oRecord = oView.Fetch

If oRecord Is Nothing Then Exit Do

iColumnCount = oRecord.FieldCount
rowData = Empty
delim = " "

For iColumn = 1 To iColumnCount
If iColumn = iColumnCount Then delim = vbLf
rowData = rowData & oRecord.StringData(iColumn) & delim
Next

strMessage = strMessage & rowData
Loop

Set oRecord = Nothing
Set oView = Nothing
Set oDB = Nothing
Set oWI = Nothing

GetMSIProperties = strMessage

End Function



'
----------------------------------------
Private Function GetArguments(SourceArray)

Dim iCount : iCount = 0

If wscript.arguments.count > 0 then

ReDim ArgArray(wscript.arguments.count -1)

For Each Argument in wscript.arguments

ArgArray(iCount) = Argument
iCount
= iCount +1
Next


iCount
= Null
GetArguments = ArgArray


End if

End Function
Which will pop up a GUI window with information about the MSI including the available properties. If you're building MSIs regularly you probably already have the relevant SDK installed and can use Orca to inspect the property table.

No comments: