我在一个PowerShell脚本中有一个小的实用函数:
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对象垃圾?
我阅读了摆脱COM对象(一劳永逸)并盲目地应用:
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($zip)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($target)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
但我不确定这是否有必要。
在本地函数中使用COM对象的正确模式是什么?
Remove-Variable
的注释,以释放对变量的任何其他引用,然后GC可能会处理其余部分。如果你的对象有一个Dispose()
方法,你可能也想在释放之前调用它。 - alroc