private void DoCloseHelper() { base.StopPipelines(); this.StopOrDisconnectAllJobs(); this.CloseOrDisconnectAllRemoteRunspaces(delegate { List <RemoteRunspace> list = new List <RemoteRunspace>(); foreach (PSSession session in this.RunspaceRepository.Runspaces) { list.Add(session.Runspace as RemoteRunspace); } return(list); }); this._engine.Context.RunspaceClosingNotification(); MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Stopped); this._engine = null; this._commandFactory = null; base.SetRunspaceState(RunspaceState.Closed); base.RaiseRunspaceStateEvents(); }
private void DoOpenHelper() { if (this._disposed) { throw PSTraceSource.NewObjectDisposedException("runspace"); } bool startLifeCycleEventWritten = false; runspaceInitTracer.WriteLine("begin open runspace", new object[0]); try { if (this.InitialSessionState != null) { this._engine = new AutomationEngine(base.Host, null, this.InitialSessionState); } else { this._engine = new AutomationEngine(base.Host, this.RunspaceConfiguration, null); } this._engine.Context.CurrentRunspace = this; MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Available); startLifeCycleEventWritten = true; this._commandFactory = new System.Management.Automation.CommandFactory(this._engine.Context); this._history = new Microsoft.PowerShell.Commands.History(this._engine.Context); this._jobRepository = new System.Management.Automation.JobRepository(); this._jobManager = new System.Management.Automation.JobManager(); this._runspaceRepository = new System.Management.Automation.RunspaceRepository(); runspaceInitTracer.WriteLine("initializing built-in aliases and variable information", new object[0]); this.InitializeDefaults(); } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); runspaceInitTracer.WriteLine("Runspace open failed", new object[0]); this.LogEngineHealthEvent(exception); if (startLifeCycleEventWritten) { MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Stopped); } base.SetRunspaceState(RunspaceState.Broken, exception); base.RaiseRunspaceStateEvents(); throw; } base.SetRunspaceState(RunspaceState.Opened); base.RunspaceOpening.Set(); base.RaiseRunspaceStateEvents(); runspaceInitTracer.WriteLine("runspace opened successfully", new object[0]); string environmentVariable = Environment.GetEnvironmentVariable("PSMODULEPATH"); if (this.InitialSessionState != null) { try { Environment.SetEnvironmentVariable("PSMODULEPATH", ModuleIntrinsics.GetSystemwideModulePath()); this.ProcessImportModule(this.InitialSessionState.CoreModulesToImport, startLifeCycleEventWritten); this._engine.Context.EngineSessionState.Module = null; } finally { Environment.SetEnvironmentVariable("PSMODULEPATH", environmentVariable); } this.ProcessImportModule(this.InitialSessionState.ModuleSpecificationsToImport, startLifeCycleEventWritten); InitialSessionState.SetSessionStateDrive(this._engine.Context, true); if (this.InitialSessionState.WarmUpTabCompletionOnIdle) { ScriptBlock action = ScriptBlock.Create("$null = [System.Management.Automation.CommandCompletion]::CompleteInput('Set-Location', 12, $null)"); this._engine.Context.Events.SubscribeEvent(null, null, "PowerShell.OnIdle", null, action, true, false, 1); } } }
private void DoCloseHelper() { base.StopPipelines(); this.StopOrDisconnectAllJobs(); this.CloseOrDisconnectAllRemoteRunspaces(delegate { List<RemoteRunspace> list = new List<RemoteRunspace>(); foreach (PSSession session in this.RunspaceRepository.Runspaces) { list.Add(session.Runspace as RemoteRunspace); } return list; }); this._engine.Context.RunspaceClosingNotification(); MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Stopped); this._engine = null; this._commandFactory = null; base.SetRunspaceState(RunspaceState.Closed); base.RaiseRunspaceStateEvents(); }
internal CommandProcessorBase CreateCommandProcessor(ExecutionContext executionContext, CommandFactory commandFactory, bool addToHistory, CommandOrigin origin) { CommandProcessorBase base2; string str2; HelpCategory category; if (!this.IsScript) { if (this._useLocalScope.HasValue && !this._useLocalScope.Value) { switch (executionContext.LanguageMode) { case PSLanguageMode.RestrictedLanguage: case PSLanguageMode.NoLanguage: throw new RuntimeException(StringUtil.Format(RunspaceStrings.UseLocalScopeNotAllowed, new object[] { "UseLocalScope", PSLanguageMode.RestrictedLanguage.ToString(), PSLanguageMode.NoLanguage.ToString() })); } } base2 = commandFactory.CreateCommand(this.CommandText, origin, this._useLocalScope); } else { if (executionContext.LanguageMode == PSLanguageMode.NoLanguage) { throw InterpreterError.NewInterpreterException(this.CommandText, typeof(ParseException), null, "ScriptsNotAllowed", ParserStrings.ScriptsNotAllowed, new object[0]); } ScriptBlock function = executionContext.Engine.ParseScriptBlock(this.CommandText, addToHistory); switch (executionContext.LanguageMode) { case PSLanguageMode.FullLanguage: case PSLanguageMode.ConstrainedLanguage: break; case PSLanguageMode.RestrictedLanguage: function.CheckRestrictedLanguage(null, null, false); break; default: throw new InvalidOperationException("Invalid langage mode was set when building a ScriptCommandProcessor"); } if (function.UsesCmdletBinding) { FunctionInfo scriptCommandInfo = new FunctionInfo("", function, executionContext); bool? nullable = this._useLocalScope; base2 = new CommandProcessor(scriptCommandInfo, executionContext, nullable.HasValue ? nullable.GetValueOrDefault() : false, false, executionContext.EngineSessionState); } else { bool? nullable2 = this._useLocalScope; base2 = new DlrScriptCommandProcessor(function, executionContext, nullable2.HasValue ? nullable2.GetValueOrDefault() : false, CommandOrigin.Runspace, executionContext.EngineSessionState); } } CommandParameterCollection parameters = this.Parameters; if (parameters != null) { bool forNativeCommand = base2 is NativeCommandProcessor; foreach (CommandParameter parameter in parameters) { CommandParameterInternal internal2 = CommandParameter.ToCommandParameterInternal(parameter, forNativeCommand); base2.AddParameter(internal2); } } if (base2.IsHelpRequested(out str2, out category)) { base2 = CommandProcessorBase.CreateGetHelpCommandProcessor(executionContext, str2, category); } this.SetMergeSettingsOnCommandProcessor(base2); return base2; }