/// <summary> /// Initializes a new instance of the PowerShellContext class and /// opens a runspace to be used for the session. /// </summary> /// <param name="hostDetails">Provides details about the host application.</param> public PowerShellContext(HostDetails hostDetails) { hostDetails = hostDetails ?? HostDetails.Default; this.psHost = new ConsoleServicePSHost(hostDetails); this.initialSessionState = InitialSessionState.CreateDefault2(); Runspace runspace = RunspaceFactory.CreateRunspace(psHost, this.initialSessionState); runspace.ApartmentState = ApartmentState.STA; runspace.ThreadOptions = PSThreadOptions.ReuseThread; runspace.Open(); this.ownsInitialRunspace = true; this.Initialize(hostDetails, runspace); // Use reflection to execute ConsoleVisibility.AlwaysCaptureApplicationIO = true; Type consoleVisibilityType = Type.GetType( "System.Management.Automation.ConsoleVisibility, System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); if (consoleVisibilityType != null) { PropertyInfo propertyInfo = consoleVisibilityType.GetProperty( "AlwaysCaptureApplicationIO", BindingFlags.Static | BindingFlags.Public); if (propertyInfo != null) { propertyInfo.SetValue(null, true); } } }
/// <summary> /// Initializes a new instance of the PowerShellContext class and /// opens a runspace to be used for the session. /// </summary> public PowerShellContext() { this.initialSessionState = InitialSessionState.CreateDefault2(); PSHost psHost = new ConsoleServicePSHost(this); Runspace runspace = RunspaceFactory.CreateRunspace(psHost, this.initialSessionState); runspace.ApartmentState = ApartmentState.STA; runspace.ThreadOptions = PSThreadOptions.ReuseThread; runspace.Open(); this.ownsInitialRunspace = true; this.Initialize(runspace); }