/// <summary> /// Called when a command string is received from the user. /// If a prompt is currently active, the prompt handler is /// asked to handle the string. Otherwise the string is /// executed in the PowerShellContext. /// </summary> /// <param name="inputString">The input string to evaluate.</param> /// <param name="echoToConsole">If true, the input will be echoed to the console.</param> public void ExecuteCommand(string inputString, bool echoToConsole) { if (this.activePromptHandler != null) { if (echoToConsole) { this.WriteOutput(inputString, true); } if (this.activePromptHandler.HandleResponse(inputString)) { // If the prompt handler is finished, clear it for // future input events this.activePromptHandler = null; } } else { // Execute the script string but don't wait for completion var executeTask = this.powerShellContext .ExecuteScriptString( inputString, echoToConsole, true) .ConfigureAwait(false); } }
private TPromptHandler GetPromptHandler <TPromptHandler>( Func <IPromptHandlerContext, TPromptHandler> factoryInvoker) where TPromptHandler : PromptHandler { if (this.activePromptHandler != null) { Logger.Write( LogLevel.Error, "Prompt handler requested while another prompt is already active."); } // Get the topmost prompt handler factory IPromptHandlerContext promptHandlerContext = this.promptHandlerContextStack.Peek(); TPromptHandler promptHandler = factoryInvoker(promptHandlerContext); this.activePromptHandler = promptHandler; this.activePromptHandler.PromptCancelled += activePromptHandler_PromptCancelled; return(promptHandler); }
private void activePromptHandler_PromptCancelled(object sender, EventArgs e) { // Clean up the existing prompt this.activePromptHandler.PromptCancelled -= activePromptHandler_PromptCancelled; this.activePromptHandler = null; }