1 つの 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 オブジェクトを使用する正しいパターンは何ですか?