My application hosts PowerShell and adds COM object references via RunSpace.SessionStateProxy.SetVariable(). After adding the variables and calling Invoke on some PowerShell code, the COM objects don't get released properly. How can I force all COM references (including those for temporary references returned by calling the COM object model) to be released by PowerShell and the .NET runtime? I tried calling
object o = rs.SessionStateProxy.GetVariable(name);
if (o != null)
{
rs.SessionStateProxy.SetVariable(name, null);
Marshal.ReleaseComObject(o);
}
on all the variables, as well PipeLine.Dispose(), Runspace.Close(), and GC.Collect(), but it didn't help.
Marshal.FinalReleaseComObjectalthough I wonder if there is some other script/command that is holding onto a reference besides the variable you null out.