我有一个小工具功能于一体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