private void ResetRunspace()

in src/PowerShell/PowerShellManager.cs [353:375]


        private void ResetRunspace()
        {
            var jobs = (List<Job2>)s_methodGetJobs.Invoke(_pwsh.Runspace.JobManager, s_argumentsGetJobs);
            if (jobs != null && jobs.Count > 0)
            {
                // Clean up jobs started during the function execution.
                _pwsh.AddCommand(Utils.RemoveJobCmdletInfo)
                        .AddParameter("Force", Utils.BoxedTrue)
                        .AddParameter("ErrorAction", "SilentlyContinue")
                     .InvokeAndClearCommands(jobs);
            }

            // We need to clean up new global variables generated from the invocation.
            // After turning 'run.ps1' to PowerShell function, if '$script:<var-name>' is used, that variable
            // will be made a global variable because there is no script scope from the file.
            //
            // We don't use 'ResetRunspaceState' because it does more than needed:
            //  - reset the current path;
            //  - reset the debugger (this causes breakpoints not work properly);
            //  - create new event manager and transaction manager;
            // We should only remove the new global variables and does nothing else.
            Utils.CleanupGlobalVariables(_pwsh);
        }