protected Runspace CreateRunspace() { Runspace runspace = null; if (this.rsConfig != null) { runspace = RunspaceFactory.CreateRunspace(this.host, this.rsConfig); } else { runspace = RunspaceFactory.CreateRunspaceFromSessionStateNoClone(this.host, this._initialSessionState); } runspace.ThreadOptions = (this.ThreadOptions == PSThreadOptions.Default) ? PSThreadOptions.ReuseThread : this.ThreadOptions; runspace.ApartmentState = this.ApartmentState; this.PropagateApplicationPrivateData(runspace); runspace.Open(); if (SystemPolicy.GetSystemLockdownPolicy() == SystemEnforcementMode.Enforce) { runspace.ExecutionContext.LanguageMode = PSLanguageMode.ConstrainedLanguage; } runspace.Events.ForwardEvent += new EventHandler <PSEventArgs>(this.OnRunspaceForwardEvent); lock (this.runspaceList) { this.runspaceList.Add(runspace); this.totalRunspaces = this.runspaceList.Count; } lock (this.syncObject) { this.cleanupTimer.Change(this.CleanupInterval, this.CleanupInterval); } this.RunspaceCreated.SafeInvoke <RunspaceCreatedEventArgs>(this, new RunspaceCreatedEventArgs(runspace)); return(runspace); }