namespace Microsoft.PowerShell.Linux.Host { using System; using System.Globalization; using System.Management.Automation.Host; using System.Management.Automation.Runspaces; /// /// This is a sample implementation of the PSHost abstract class for /// console applications. Not all members are implemented. Those that /// are not implemented throw a NotImplementedException exception or /// return nothing. /// internal class MyHost : PSHost, IHostSupportsInteractiveSession { public MyHost(Listener Listener) { this.Listener = Listener; } /// /// A reference to the PSHost implementation. /// private Listener Listener; /// /// The culture information of the thread that created /// this object. /// private CultureInfo originalCultureInfo = CultureInfo.CurrentCulture; /// /// The UI culture information of the thread that created /// this object. /// private CultureInfo originalUICultureInfo = CultureInfo.CurrentCulture; /// /// The identifier of this PSHost implementation. /// private static Guid instanceId = Guid.NewGuid(); /// /// A reference to the implementation of the PSHostUserInterface /// class for this application. /// private MyHostUserInterface myHostUserInterface = new MyHostUserInterface(); /// /// A reference to the runspace used to start an interactive session. /// public Runspace pushedRunspace = null; /// /// Gets the culture information to use. This implementation /// returns a snapshot of the culture information of the thread /// that created this object. /// public override CultureInfo CurrentCulture { get { return this.originalCultureInfo; } } /// /// Gets the UI culture information to use. This implementation /// returns a snapshot of the UI culture information of the thread /// that created this object. /// public override CultureInfo CurrentUICulture { get { return this.originalUICultureInfo; } } /// /// Gets an identifier for this host. This implementation always /// returns the GUID allocated at instantiation time. /// public override Guid InstanceId { get { return instanceId; } } /// /// Gets a string that contains the name of this host implementation. /// Keep in mind that this string may be used by script writers to /// identify when your host is being used. /// public override string Name { get { return "MySampleConsoleHostImplementation"; } } /// /// Gets an instance of the implementation of the PSHostUserInterface /// class for this application. This instance is allocated once at startup time /// and returned every time thereafter. /// public override PSHostUserInterface UI { get { return this.myHostUserInterface; } } /// /// Gets the version object for this application. Typically this /// should match the version resource in the application. /// public override Version Version { get { return new Version(1, 0, 0, 0); } } #region IHostSupportsInteractiveSession Properties /// /// Gets a value indicating whether a request /// to open a PSSession has been made. /// public bool IsRunspacePushed { get { return this.pushedRunspace != null; } } /// /// Gets or sets the runspace used by the PSSession. /// public Runspace Runspace { get { return this.Listener.myRunSpace; } internal set { this.Listener.myRunSpace = value; } } #endregion IHostSupportsInteractiveSession Properties /// /// This API Instructs the host to interrupt the currently running /// pipeline and start a new nested input loop. In this example this /// functionality is not needed so the method throws a /// NotImplementedException exception. /// public override void EnterNestedPrompt() { throw new NotImplementedException( "The method or operation is not implemented."); } /// /// This API instructs the host to exit the currently running input loop. /// In this example this functionality is not needed so the method /// throws a NotImplementedException exception. /// public override void ExitNestedPrompt() { throw new NotImplementedException( "The method or operation is not implemented."); } /// /// This API is called before an external application process is /// started. Typically it is used to save state so that the parent /// can restore state that has been modified by a child process (after /// the child exits). In this example this functionality is not /// needed so the method returns nothing. /// public override void NotifyBeginApplication() { return; } /// /// This API is called after an external application process finishes. /// Typically it is used to restore state that a child process has /// altered. In this example, this functionality is not needed so /// the method returns nothing. /// public override void NotifyEndApplication() { return; } /// /// Indicate to the host application that exit has /// been requested. Pass the exit code that the host /// application should use when exiting the process. /// /// The exit code that the /// host application should use. public override void SetShouldExit(int exitCode) { this.Listener.ShouldExit = true; this.Listener.ExitCode = exitCode; } #region IHostSupportsInteractiveSession Methods /// /// Requests to close a PSSession. /// public void PopRunspace() { Runspace = this.pushedRunspace; this.pushedRunspace = null; } /// /// Requests to open a PSSession. /// /// Runspace to use. public void PushRunspace(Runspace runspace) { this.pushedRunspace = Runspace; Runspace = runspace; } #endregion IHostSupportsInteractiveSession Methods } }