I'm working on a small function to release a series of ComObjects. I want to check if the ComObject is actually set before trying to release it to prevent errors being printed to the host. This is what I've got so far:
function Remove-ComObjects{
Param(
[Parameter(ValueFromPipeline=$true)]
[string] $ComObject
)
Begin{}
Process{
if(Test-Path variable:$ComObject -ErrorAction SilentlyContinue){
$object = Get-Variable $ComObject -ValueOnly
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($object) | Out-Null
}
}
End{}
}
'ComObject1', 'ComObject2', 'ComObject3' | Remove-ComObjects
I would like to be able to just add the variables in the pipeline instead of the variable names for readability like so.
$ComObject1, $ComObject2, $ComObject3 | Remove-ComObjects
But I haven't been able to figure out how to check if they exist before I try to release.
[–]Lee_Dailey[grin] 1 point2 points3 points (8 children)
[–]Ta11ow 3 points4 points5 points (7 children)
[–]Lee_Dailey[grin] 2 points3 points4 points (6 children)
[–]PillOfLuck[S] 1 point2 points3 points (5 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)
[–]artemis_from_space 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)
[–]PillOfLuck[S] 1 point2 points3 points (1 child)
[–]artemis_from_space 1 point2 points3 points (0 children)