public ClientMessage[] SetPromptForChoiceReply(int reply) { ClientMessage[] clientMessages; PowwaEvents.PowwaEVENT_DEBUG_LOG1("SetPromptForChoiceReply(): Enter", "reply", reply.ToString(CultureInfo.InvariantCulture)); try { lock (this.clientRequestLock) { lock (this.sessionStateLock) { PromptForChoiceMessage promptForChoiceMessage = this.ValidateSessionStateForMessageReply <PromptForChoiceMessage>(); if (reply < 0 || reply >= promptForChoiceMessage.Choices.Count) { int count = promptForChoiceMessage.Choices.Count; PowwaEvents.PowwaEVENT_DEBUG_LOG2("SetPromptForChoiceReply(): Choice not within the valid range", "reply", reply.ToString(CultureInfo.InvariantCulture), "message.Choices.Count", count.ToString(CultureInfo.InvariantCulture)); throw new ArgumentException("The choice is not within the valid range", "reply"); } else { this.messageQueue.SetInputMessageReply(reply); } } clientMessages = this.GetClientMessages(); } } finally { PowwaEvents.PowwaEVENT_DEBUG_LOG0("SetPromptForChoiceReply(): Exit"); } return(clientMessages); }
public string[] GetTabCompletion(string commandLine) { string[] strArrays; if (commandLine != null) { lock (this.clientRequestLock) { if (this.State == PowwaSession.SessionState.Available) { this.State = PowwaSession.SessionState.ExecutingCommand; System.Management.Automation.PowerShell powerShell = null; try { try { powerShell = System.Management.Automation.PowerShell.Create(); powerShell.Runspace = this.Runspace; CommandCompletion commandCompletion = CommandCompletion.CompleteInput(commandLine, commandLine.Length, null, powerShell); string str = commandLine.Substring(0, commandCompletion.ReplacementIndex); string[] strArrays1 = new string[commandCompletion.CompletionMatches.Count]; for (int i = 0; i < commandCompletion.CompletionMatches.Count; i++) { strArrays1[i] = string.Concat(str, commandCompletion.CompletionMatches[i].CompletionText); } strArrays = strArrays1; } catch { strArrays = new string[0]; } } finally { if (powerShell != null) { powerShell.Dispose(); } this.State = PowwaSession.SessionState.Available; } } else { PowwaEvents.PowwaEVENT_DEBUG_LOG1("GetTabCompletion(): Invalid Session State", "SessionState", this.State.ToString()); throw new InvalidOperationException("The session is not available"); } } return(strArrays); } else { throw new ArgumentNullException("commandLine"); } }
public ClientMessage[] GetClientMessages() { ClientMessage[] messages; lock (this.clientRequestLock) { bool flag = false; lock (this.sessionStateLock) { PowwaSession.SessionState state = this.State; switch (state) { case PowwaSession.SessionState.Available: { break; } case PowwaSession.SessionState.ExecutingCommand: case PowwaSession.SessionState.CancellingCommand: { flag = true; break; } case PowwaSession.SessionState.Closed: { throw PowwaException.CreateInvalidSessionException(); } default: { break; } } } if (flag) { this.messageQueue.WaitForMessages(); } lock (this.sessionStateLock) { if (this.State != PowwaSession.SessionState.Closed) { messages = this.messageQueue.GetMessages(); } else { PowwaEvents.PowwaEVENT_DEBUG_LOG1("GetClientMessages() Invalid Session State", "SessionState", this.State.ToString()); throw PowwaException.CreateInvalidSessionException(); } } } return(messages); }
public ClientConfiguration GetClientConfiguration() { ClientConfiguration clientConfiguration; string computerName; PowwaEvents.PowwaEVENT_DEBUG_LOG0("GetClientConfiguration(): Enter"); try { lock (this.clientRequestLock) { if (this.State == PowwaSession.SessionState.Available) { ClientConfiguration bufferSize = new ClientConfiguration(); bufferSize.BufferSize = this.host.UI.RawUI.BufferSize; bufferSize.WindowSize = this.host.UI.RawUI.WindowSize; bufferSize.ForegroundColor = HtmlHelper.ToHtmlColor(this.host.UI.RawUI.ForegroundColor); bufferSize.BackgroundColor = HtmlHelper.ToHtmlColor(this.host.UI.RawUI.BackgroundColor); bufferSize.WindowTitle = this.host.UI.RawUI.WindowTitle; bufferSize.Prompt = this.GetPowerShellPrompt(); ClientConfiguration clientConfiguration1 = bufferSize; if (this.Runspace.ConnectionInfo != null) { computerName = this.Runspace.ConnectionInfo.ComputerName; } else { computerName = string.Empty; } clientConfiguration1.ComputerName = computerName; bufferSize.InputForegroundColor = HtmlHelper.ToHtmlColor(ConsoleColor.Yellow); bufferSize.InputBackgroundColor = HtmlHelper.ToHtmlColor(ConsoleColor.DarkBlue); bufferSize.ErrorForegroundColor = HtmlHelper.ToHtmlColor(ConsoleColor.Red); bufferSize.ErrorBackgroundColor = HtmlHelper.ToHtmlColor(ConsoleColor.Black); bufferSize.WarningForegroundColor = HtmlHelper.ToHtmlColor(ConsoleColor.Yellow); bufferSize.WarningBackgroundColor = HtmlHelper.ToHtmlColor(ConsoleColor.Black); bufferSize.StartupMessages = this.GetClientMessages(); clientConfiguration = bufferSize; } else { PowwaEvents.PowwaEVENT_DEBUG_LOG1("GetClientConfiguration(): Invalid Session State", "SessionState", this.State.ToString()); throw new InvalidOperationException("The session is not available"); } } } finally { PowwaEvents.PowwaEVENT_DEBUG_LOG0("GetClientConfiguration(): Exit"); } return(clientConfiguration); }
private T ValidateSessionStateForMessageReply <T>() where T : ClientMessage { /* this.State != PowwaSession.SessionState.ExecutingCommand || */ /* TODO: The command is reset to Available... */ if (!this.WaitingForUserReply) { PowwaEvents.PowwaEVENT_DEBUG_LOG1("ValidateSessionStateForMessageReply(): Session not waiting for reply", "SessionState", this.State.ToString()); throw new InvalidOperationException("The session is not waiting for a reply"); } else { T pendingInputMessage = (T)(this.messageQueue.PendingInputMessage as T); if (pendingInputMessage != null) { return(pendingInputMessage); } else { throw new InvalidOperationException("Invalid reply type"); } } }
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) { PSCredential reply; string str; PowwaEvents.PowwaEVENT_DEBUG_LOG0("PromptForCredential(): Enter"); try { if (string.IsNullOrEmpty(targetName)) { bool flag = (allowedCredentialTypes & PSCredentialTypes.Domain) == PSCredentialTypes.Domain; string str1 = "PromptForCredential()"; string str2 = "domainCredentials"; if (flag) { str = "true"; } else { str = "false"; } PowwaEvents.PowwaEVENT_DEBUG_LOG1(str1, str2, str); MessageCreatedEventArgs messageCreatedEventArg = new MessageCreatedEventArgs(new PromptForCredentialMessage(caption, message, userName, flag), true); this.OnMessageCreated(messageCreatedEventArg); reply = (PSCredential)messageCreatedEventArg.Reply; } else { PowwaEvents.PowwaEVENT_DEBUG_LOG1("PromptForCredential(): Invalid Argument", "targetName", "null"); throw new NotSupportedException(Resources.PromptForCredentialTargetNameNotSupported); } } finally { PowwaEvents.PowwaEVENT_DEBUG_LOG0("PromptForCredential(): Exit"); } return(reply); }
public ClientMessage[] ExecuteCommand(string command) { ClientMessage[] clientMessages; if (command != null) { lock (this.clientRequestLock) { lock (this.sessionStateLock) { if (this.State == PowwaSession.SessionState.Available) { PSCommand commands = null; try { commands = ScriptBlock.Create(command).GetPowerShell(new object[0]).Commands; } catch (ScriptBlockToPowerShellNotSupportedException scriptBlockToPowerShellNotSupportedException) { } catch (RuntimeException runtimeException) { } if (commands == null) { commands = new PSCommand(); commands.AddScript(command); } commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output); Command outputCmd = new Command("Out-Default", false, false); commands.AddCommand(outputCmd); this.executingCommandPowerShell.Commands = commands; this.executingCommandPowerShell.HistoryString = command; try { PSInvocationSettings pSInvocationSetting = new PSInvocationSettings(); pSInvocationSetting.AddToHistory = true; /*Exception ex; * this.State = PowwaSession.SessionState.ExecutingCommand; * this.exec.ExecuteCommandAsync(command, out ex, Microsoft.PowerShell.Executor.ExecutionOptions.AddOutputter | Microsoft.PowerShell.Executor.ExecutionOptions.AddToHistory); * if (ex != null) throw ex; */ this.executingCommandPowerShell.BeginInvoke <object>(null, pSInvocationSetting, null, null); } catch (Exception exception1) { Exception exception = exception1; this.WriteException(exception); if (exception as InvalidRunspaceStateException != null) { this.PostClientMessage(new SessionTerminatedMessage(), false); } } } else { PowwaEvents.PowwaEVENT_DEBUG_LOG1("ExecuteCommand(): Invalid Session State", "SessionState", this.State.ToString()); throw new InvalidOperationException("The session is not available"); } } clientMessages = this.GetClientMessages(); } return(clientMessages); } else { throw new ArgumentNullException("command"); } }
public override Dictionary <string, PSObject> Prompt(string caption, string message, Collection <FieldDescription> descriptions) { bool flag = false; Dictionary <string, PSObject> reply; PowwaEvents.PowwaEVENT_DEBUG_LOG0("Prompt(): Enter"); try { if (descriptions != null) { if (descriptions.Count != 0) { PromptFieldDescription[] promptFieldDescriptionArray = new PromptFieldDescription[descriptions.Count]; int num = 0; while (num < descriptions.Count) { if (descriptions[num] != null) { Type fieldType = PowwaHostUserInterface.GetFieldType(descriptions[num], out flag); PromptFieldType promptFieldType = PromptFieldType.String; if (fieldType != typeof(SecureString)) { if (fieldType == typeof(PSCredential)) { promptFieldType = PromptFieldType.Credential; } } else { promptFieldType = PromptFieldType.SecureString; } PromptFieldDescription promptFieldDescription = new PromptFieldDescription(); promptFieldDescription.Name = descriptions[num].Name; promptFieldDescription.Label = descriptions[num].Label; promptFieldDescription.HelpMessage = descriptions[num].HelpMessage; promptFieldDescription.PromptFieldType = promptFieldType; promptFieldDescription.PromptFieldTypeIsList = flag; promptFieldDescriptionArray[num] = promptFieldDescription; num++; } else { object[] objArray = new object[1]; objArray[0] = num; string str = string.Format(CultureInfo.InvariantCulture, "descriptions[{0}]", objArray); PowwaEvents.PowwaEVENT_DEBUG_LOG1("Prompt(): Invalid Description", str, "null"); throw new ArgumentNullException(str); } } MessageCreatedEventArgs messageCreatedEventArg = new MessageCreatedEventArgs(new PromptMessage(caption, message, promptFieldDescriptionArray), true); this.OnMessageCreated(messageCreatedEventArg); reply = (Dictionary <string, PSObject>)messageCreatedEventArg.Reply; } else { int count = descriptions.Count; PowwaEvents.PowwaEVENT_DEBUG_LOG1("Prompt(): Invalid Argument", "Descriptions.Count", count.ToString(CultureInfo.InvariantCulture)); throw new ArgumentException("descriptions cannot be an empty array", "descriptions"); } } else { PowwaEvents.PowwaEVENT_DEBUG_LOG1("Prompt(): Invalid Argument", "Descriptions", "null"); throw new ArgumentNullException("descriptions"); } } finally { PowwaEvents.PowwaEVENT_DEBUG_LOG0("Prompt(): Exit"); } return(reply); }