PowerShell+MSI. How to release opened database?
I have VB Script:
.....
Set oInstaller = CreateObject("WindowsInstall开发者_如何学Cer.Installer")
Set otempDB = oInstaller.OpenDatabase(sMsiFullPathTemp, 1)
.......
Set otempDB = Nothing
At this string it releases database: Set otempDB = Nothing.
I need to make PowerShell script like this VBS to work with MSI.
PowerShell:
....
function Invoke-Method ($Object, $MethodName, $ArgumentList) {
return $Object.GetType().InvokeMember($MethodName, 'Public, Instance, InvokeMethod', $null, $Object, $ArgumentList)
}
$oInstaller = New-Object -ComObject WindowsInstaller.Installer
$otempDB = Invoke-Method $oInstaller OpenDatabase @($tempmsi, 1)
But how I can release MSI database until script haven't finished work? I mean something like: Set otempDB = Nothing
Could someone help me with it?
Thanks
you can always assign the variable to $null:
$otempDB = $null
However for COM objects this often results in the object just being shuffled off to limbo somewhere. Better is to explicitly relese the object and clean up the memory:
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$otempDB) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Somewhere I came across a function for this some time ago (if someone knows who originally posted this function please leave a comment and I will attribute it). I use it pretty much any time that I work with COM objects:
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
精彩评论