Installing MSI several times

One of the features of Windows MSI is that it can determine what version of the product is installed already and upgrade it if necessary. It uses unique product code guid to identify your product among all installed programs. This implies that product can be installed only once. But what if you have a requirement to install the same product several times on the same machine?
Microsoft suggests that you use instance transforms to do that. But this means that for each installation you need to ship a separate instance transform which is not suitable in my case.
I came up with the following script that modifies msi product code and allows you to install it as many times as you want. Each installation is identified by a unique instance name. You specify it as an Instance parameter to the powershell script below. wirunsql.vbs and wisuminf.vbs are part of Windows SDK: https://www.microsoft.com/en-us/download/details.aspx?id=3138

param (
[Parameter(Mandatory=$false)][string]$Instance
)
[string]$DefaultInstance = "MyProduct_MSI"
if ($Instance) {
Write-Host "Copying installer to $($Instance).msi"
Copy-Item "MyProduct_MSI.msi" "$($Instance).msi"
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$hash = (new-object -TypeName System.Guid -ArgumentList @(,$md5.ComputeHash($utf8.GetBytes($Instance)))).ToString().ToUpper()
Write-Host "Product code: $($hash)"
Write-Host "Injecting product code into $($Instance).msi"
Invoke-Expression -Command:"cscript wirunsql.vbs $($Instance).msi ""UPDATE Property SET Value='{$($hash)}' WHERE Property='ProductCode'"""
Invoke-Expression -Command:"cscript wirunsql.vbs $($Instance).msi ""UPDATE Property SET Value='MyProduct ($($Instance))' WHERE Property='ProductName'"""
Invoke-Expression -Command:"cscript wirunsql.vbs $($Instance).msi ""UPDATE Property SET Value='{$($hash)}' WHERE Property='UpgradeCode'"""
Invoke-Expression -Command:"cscript wisuminf.vbs $($Instance).msi Revision='{$($hash)}'"
}
else {
$Instance = $DefaultInstance
}
Write-Host "Launching installer package..."
Invoke-Expression -Command:"msiexec /i $($Instance).msi"
view raw InstallMsi.ps1 hosted with ❤ by GitHub

Comments

Popular posts from this blog

TFS Proxy not working: The source control proxy is not responding

Demystifying fast up-to-date check in Visual Studio C# projects