0

I am creating a PowerShell script to generate a MST from MSI with creating a new component, features and properties. While creating the component, I am facing a issue. Below is my code snippet and the error message.

Code:

$windowsInstaller = New-Object -ComObject WindowsInstaller.Installer             
$database1 = $windowsInstaller.GetType().InvokeMember(
    "OpenDatabase", 
    "InvokeMethod", 
    $Null, 
    $windowsInstaller, 
    @($database1Path, $msiOpenDatabaseModeReadOnly)
)  


#open 'backup' MSI in transact mode
$database2 = $windowsInstaller.GetType().InvokeMember(
    "OpenDatabase", 
    "InvokeMethod", 
    $Null, 
    $windowsInstaller, 
    @($database2Path, $msiOpenDatabaseModeTransact)
) 

$query2 = "INSERT INTO ``Component`` (``Component``,``ComponentId``,``Directory_``,``Attributes``,``Condition``,``KeyPath``,``Full Directory``) VALUES ('My_Branding','{B960EF8B-A4AC-447C-91F3-C19CCAC3899E}','INSTALLDIR','4',' ',' ','Windows\System32\Macromed\Flash')"
$View2 = $database2.GetType().InvokeMember("OpenView","InvokeMethod",$Null,$database2,($query2))
$View2.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View2, $Null)      
$View2.GetType().InvokeMember("Close", "InvokeMethod", $Null, $View2, $Null)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($View2) | Out-Null

#Commit the changes to our backup database
$database2.GetType().InvokeMember("Commit", "InvokeMethod", $Null, $database2, $Null)


#Generate a transform (the difference between our original MSI and our Backup MSI)  
$transformSuccess = $database2.GetType().InvokeMember(
    "GenerateTransform", 
    "InvokeMethod", 
    $Null, 
    $database2, 
    @($database1,$MSTPath)
)  

#Create a Summary Information Stream for the MST
$transformSummarySuccess = $database2.GetType().InvokeMember(
    "CreateTransformSummaryInfo", 
    "InvokeMethod", 
    $Null, 
    $database2, 
    @($database1,$MSTPath, $msiTransformErrorNone, $msiTransformValidationNone)
) 



#Release objects from memory
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($database1) | Out-Null 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($database2) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($windowsInstaller) | Out-Null

Error Message:

Exception calling "InvokeMember" with "5" argument(s): "OpenView,Sql" At C:\Jagan\Jagan\Scripts\New folder\MSI\Untitled5.ps1:65 char:1 + $View2 = $database2.GetType().InvokeMember("OpenView","InvokeMethod",$Null,$data ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : COMException Exception calling "InvokeMember" with "5" argument(s): "COM object that has been separated from its underlying RCW cannot be used." At C:\Jagan\Jagan\Scripts\New folder\MSI\Untitled5.ps1:66 char:1 + $View2.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View2, $Null) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidComObjectException Exception calling "InvokeMember" with "5" argument(s): "COM object that has been separated from its underlying RCW cannot be used." At C:\Jagan\Jagan\Scripts\New folder\MSI\Untitled5.ps1:67 char:1 + $View2.GetType().InvokeMember("Close", "InvokeMethod", $Null, $View2, $Null) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidComObjectException

BenH
  • 9,766
  • 1
  • 22
  • 35
  • Consider using [WiX toolset](http://wix.codeplex.com/) that adds Microsoft.Deployment.WindowsInstaller .NET namespace. – wOxxOm Jun 06 '17 at 17:56
  • Perhaps this would be of help - https://stackoverflow.com/questions/5544844/how-to-call-a-complex-com-method-from-powershell – yossiz74 Jun 07 '17 at 10:55

0 Answers0