我有一個小工具功能於一體PowerShell腳本:PowerShell中釋放COM對象
function Unzip-File{
param(
[Parameter(Mandatory=$true)]
[string]$ZipFile,
[Parameter(Mandatory=$true)]
[string]$Path
)
$shell=New-Object -ComObject shell.application
$zip = $shell.namespace($ZipFile)
$target = $shell.NameSpace($Path)
$target.CopyHere($zip.Items())
}
我應該清理腳本中的COM對象?還是PowerShell足夠智能地自動垃圾COM對象?
我讀過Getting Rid of a COM Object (Once and For All)和盲目應用:
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($zip)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($target)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
但我不知道這是否需要。
在本地函數中使用COM對象的正確模式是什麼?
我認爲你找到的文章可能是正確的。對「Remove-Variable」的註釋進一步閱讀以釋放對變量的任何其他引用,然後GC可能會處理其餘的部分。如果你的對象有一個'Dispose()'方法,你也可以調用它(在釋放之前)。 – alroc