public void RunScript(string script) { if ((this.showCommandProxy != null) && !string.IsNullOrEmpty(script)) { if (this.passThrough) { base.WriteObject(script); } else if (this.errorPopup != 0) { this.RunScriptSilentlyAndWithErrorHookup(script); } else if (this.showCommandProxy.HasHostWindow) { if (!this.showCommandProxy.SetPendingISECommand(script)) { this.RunScriptSilentlyAndWithErrorHookup(script); } } else if (!ConsoleInputWithNativeMethods.AddToConsoleInputBuffer(script, true)) { base.WriteDebug(FormatAndOut_out_gridview.CannotWriteToConsoleInputBuffer); this.RunScriptSilentlyAndWithErrorHookup(script); } } }
/// <summary> /// Executes a PowerShell script, writing the output objects to the pipeline. /// </summary> /// <param name="script">Script to execute</param> public void RunScript(string script) { if (_showCommandProxy == null || string.IsNullOrEmpty(script)) { return; } if (_passThrough) { this.WriteObject(script); return; } if (ErrorPopup) { this.RunScriptSilentlyAndWithErrorHookup(script); return; } if (_showCommandProxy.HasHostWindow) { if (!_showCommandProxy.SetPendingISECommand(script)) { this.RunScriptSilentlyAndWithErrorHookup(script); } return; } if (!ConsoleInputWithNativeMethods.AddToConsoleInputBuffer(script, true)) { this.WriteDebug(FormatAndOut_out_gridview.CannotWriteToConsoleInputBuffer); this.RunScriptSilentlyAndWithErrorHookup(script); } }
/// <summary> /// Adds a string to the console input buffer. /// </summary> /// <param name="str">String to add to console input buffer.</param> /// <param name="newLine">True to add Enter after the string.</param> /// <returns>True if it was successful in adding all characters to console input buffer.</returns> internal static bool AddToConsoleInputBuffer(string str, bool newLine) { IntPtr handle = ConsoleInputWithNativeMethods.GetStdHandle(ConsoleInputWithNativeMethods.STD_INPUT_HANDLE); if (handle == IntPtr.Zero) { return(false); } uint strLen = (uint)str.Length; ConsoleInputWithNativeMethods.INPUT_RECORD[] records = new ConsoleInputWithNativeMethods.INPUT_RECORD[strLen + (newLine ? 1 : 0)]; for (int i = 0; i < strLen; i++) { ConsoleInputWithNativeMethods.INPUT_RECORD.SetInputRecord(ref records[i], str[i]); } uint written; if (!ConsoleInputWithNativeMethods.WriteConsoleInput(handle, records, strLen, out written) || written != strLen) { // I do not know of a case where written is not going to be strlen. Maybe for some character that // is not supported in the console. The API suggests this can happen, // so we handle it by returning false return(false); } // Enter is written separately, because if this is a command, and one of the characters in the command was not written // (written != strLen) it is desireable to fail (return false) before typing enter and running the command if (newLine) { ConsoleInputWithNativeMethods.INPUT_RECORD[] enterArray = new ConsoleInputWithNativeMethods.INPUT_RECORD[1]; ConsoleInputWithNativeMethods.INPUT_RECORD.SetInputRecord(ref enterArray[0], (char)13); written = 0; if (!ConsoleInputWithNativeMethods.WriteConsoleInput(handle, enterArray, 1, out written)) { // I don't think this will happen return(false); } Diagnostics.Assert(written == 1, "only Enter is being added and it is a supported character"); } return(true); }
/// <summary> /// Executes a PowerShell script, writing the output objects to the pipeline. /// </summary> /// <param name="script">Script to execute.</param> public void RunScript(string script) { if (_showCommandProxy == null || string.IsNullOrEmpty(script)) { return; } if (_passThrough) { this.WriteObject(script); return; } if (ErrorPopup) { this.RunScriptSilentlyAndWithErrorHookup(script); return; } if (_showCommandProxy.HasHostWindow) { if (!_showCommandProxy.SetPendingISECommand(script)) { this.RunScriptSilentlyAndWithErrorHookup(script); } return; } // Don't send newline at end as PSReadLine shows it rather than executing if (!ConsoleInputWithNativeMethods.AddToConsoleInputBuffer(script, newLine: false)) { this.WriteDebug(FormatAndOut_out_gridview.CannotWriteToConsoleInputBuffer); this.RunScriptSilentlyAndWithErrorHookup(script); } }