コード例 #1
0
 /// <summary>
 /// Reads a command when Enter is pressed.
 /// </summary>
 /// <param name="sender">sender</param>
 /// <param name="e">event</param>
 private void HandleEnterCommand(object sender, KeyEventArgs e)
 {
     try {
         lock (this.instanceLock) {
             string cmd = PowerShellConsolePrinter.ReadLine(((MyHostUserInterface)this.myHost.UI).ConsolePromptDefinator);
             if (!String.IsNullOrEmpty(cmd))
             {
                 this.Execute(cmd);
             }
         }
     }
     catch (Exception exception) {
         this.myHost.UI.WriteErrorLine(exception.ToString());
     }
 }
コード例 #2
0
        /// <summary>
        /// Implements the basic listener loop. It sets up the ctrl-C handler, then
        /// reads a command from the user, executes it and repeats until the ShouldExit
        /// flag is set.
        /// </summary>
        public void Run()
        {
            // Set up the control-C handler.

            PowerShellConsolePrinter.CtrlCHandler += new KeyEventHandler(this.HandleControlC);
            //PowerShellConsolePrinter.EnterKeyHandler += new KeyEventHandler(this.HandleEnterCommand);
            //Console.TreatControlCAsInput = false;

            //this.currentPowerShell.BeginStop
            //this.currentPowerShell.Runspace = this.myRunSpace;

            // loop reading commands to execute until ShouldExit is set by
            // the user calling "exit".
            string prompt;

            if (this.myHost.IsRunspacePushed)
            {
                prompt = string.Format("[{0}] PS>", this.myRunSpace.ConnectionInfo.ComputerName);
            }
            else
            {
                prompt = "PS>";
            }
            ((MyHostUserInterface)this.myHost.UI).ConsolePromptDefinator = String.IsNullOrEmpty(prompt) ? "PS>" : prompt;
            //this.myHost.UI.Write(ConsoleColor.Cyan, ConsoleColor.Black, prompt);

            while (!this.ShouldExit)
            {
                System.Threading.Thread.Sleep(100);
                string cmd = PowerShellConsolePrinter.ReadLine(prompt);
                if (!String.IsNullOrEmpty(cmd))
                {
                    lock (this.instanceLock) {
                        this.Execute(cmd);
                    }
                }
            }

            // Exit with the desired exit code that was set by exit command.
            // This is set in the host by the MyHost.SetShouldExit() implementation.
            //Environment.Exit(this.ExitCode);
        }
コード例 #3
0
        /// <summary>
        /// Prompts the user for input.
        /// </summary>
        /// <param name="caption">Text that preceeds the prompt (a title).</param>
        /// <param name="message">Text of the prompt.</param>
        /// <param name="descriptions">A collection of FieldDescription objects
        /// that contains the user input.</param>
        /// <returns>A dictionary object that contains the results of the user prompts.</returns>
        public override Dictionary <string, PSObject> Prompt(
            string caption,
            string message,
            Collection <FieldDescription> descriptions)
        {
            this.Write(ConsoleColor.Blue, ConsoleColor.Black, caption + "\n" + message + " ");


            Dictionary <string, PSObject> results = new Dictionary <string, PSObject>();

            foreach (FieldDescription fd in descriptions)
            {
                string[]     label     = GetHotkeyAndLabel(fd.Label);
                SecureString secureStr = new SecureString();

                this.WriteLine(label[1]);
                string userData = null;
                if (String.IsNullOrEmpty(caption) && String.IsNullOrEmpty(message))
                {
                    userData = null;
                    this.Write(ConsoleColor.Blue, ConsoleColor.Black, fd.Name + ":>");
                    //PowerShellConsolePrinter.WriteDelayedMessage(ConsolePromptDefinator);
                    while (userData == null)
                    {
                        System.Threading.Thread.Sleep(100);
                        userData = PowerShellConsolePrinter.ReadLine(":>");
                        if (String.IsNullOrEmpty(userData))
                        {
                            userData = null;
                        }
                    }
                    if (!String.IsNullOrEmpty(userData))
                    {
                        userData = userData.Trim(Environment.NewLine.ToCharArray());
                    }
                    PowerShellConsolePrinter.RemoveLines(new List <string>()
                    {
                        ConsolePromptDefinator
                    });
                    if (fd.ParameterTypeName == "SecureString" && !String.IsNullOrEmpty(userData))
                    {
                        foreach (char c in userData.ToCharArray())
                        {
                            secureStr.AppendChar(c);
                        }
                        results[fd.Name] = PSObject.AsPSObject(secureStr);
                    }
                }

                if (userData == null)
                {
                    return(null);
                }
                if (fd.ParameterTypeName != "SecureString")
                {
                    results[fd.Name] = PSObject.AsPSObject(userData);
                }
            }

            return(results);
        }
コード例 #4
0
 /// <summary>
 /// Reads characters that are entered by the user until a
 /// newline (carriage return) is encountered.
 /// </summary>
 /// <returns>The characters entered by the user.</returns>
 public override string ReadLine()
 {
     return(PowerShellConsolePrinter.ReadLine(ConsolePromptDefinator));
 }