내가 한 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 개체를 사용하는 올바른 패턴은 무엇입니까? 나는 나의하여 ComObject 항상 살아있어 주목했기 때문에
function Release-Ref ($ref) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) | out-null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
, 나는 파워 쉘 2.0은 더 이상 사용하여 ComObject를 제거 할 수 없습니다 생각 :
내가 아는 기사가 맞을 것으로 생각합니다. 변수에 대한 다른 참조를 해제하기 위해'Remove-Variable'의 주석을 조금 읽은 다음 나머지는 GC가 처리합니다. 당신의 객체가'Dispose()'메소드를 가지고 있다면 당신은 그것을 호출하기를 원할지도 모른다. – alroc