diff --git a/src/PowerShellEditorServices/Console/ConsoleReadLine.cs b/src/PowerShellEditorServices/Console/ConsoleReadLine.cs index d2e77c332..bbe91ea65 100644 --- a/src/PowerShellEditorServices/Console/ConsoleReadLine.cs +++ b/src/PowerShellEditorServices/Console/ConsoleReadLine.cs @@ -303,10 +303,11 @@ private async Task ReadLine(bool isCommandLine, CancellationToken cancel command.AddCommand("Get-History"); currentHistory = - await this.powerShellContext.ExecuteCommand( - command, - false, - false) as Collection; + new Collection( + (await this.powerShellContext.ExecuteCommand( + command, + false, + false)).ToList()); if (currentHistory != null) { diff --git a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs index 2199e1839..d2f99e287 100644 --- a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs @@ -33,8 +33,10 @@ public IEnumerable ExecuteCommandInDebugger( { IEnumerable executionResult = null; - using (var nestedPipeline = currentRunspace.CreateNestedPipeline()) + string historyString = psCommand.Commands[0].CommandText; + using (var nestedPipeline = currentRunspace.CreateNestedPipeline(historyString, sendOutputToHost)) { + nestedPipeline.Commands.Clear(); foreach (var command in psCommand.Commands) { nestedPipeline.Commands.Add(command); diff --git a/src/PowerShellEditorServices/Session/PowerShell4Operations.cs b/src/PowerShellEditorServices/Session/PowerShell4Operations.cs index ea4070225..61ca86aa8 100644 --- a/src/PowerShellEditorServices/Session/PowerShell4Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell4Operations.cs @@ -13,6 +13,15 @@ namespace Microsoft.PowerShell.EditorServices.Session { internal class PowerShell4Operations : IVersionSpecificOperations { + private static SortedSet s_noHistoryCommandNames = new SortedSet(StringComparer.OrdinalIgnoreCase) + { + "prompt", + "Set-PSDebuggerAction", + "Get-PSDebuggerStopArgs", + "Set-PSDebugMode", + "TabExpansion2" + }; + public void ConfigureDebugger(Runspace runspace) { #if !PowerShellv3 @@ -54,6 +63,24 @@ public IEnumerable ExecuteCommandInDebugger( }; } + // There's no way to tell the debugger not to add the command to history. It does however, + // check if the first command is in a static list of commands that shouldn't be added + // to history. We use that here to get around that limitation. + if (!sendOutputToHost && !s_noHistoryCommandNames.Contains(psCommand.Commands[0].CommandText)) + { + var newCommand = new PSCommand() + .AddCommand("prompt") + .AddCommand("Microsoft.PowerShell.Core\\Out-Null") + .AddStatement(); + + foreach (Command command in psCommand.Commands) + { + newCommand.AddCommand(command); + } + + psCommand = newCommand; + } + DebuggerCommandResults commandResults = currentRunspace.Debugger.ProcessCommand( psCommand, diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 0c6e1ee98..a157dcb4e 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -1490,10 +1490,12 @@ private SessionDetails GetSessionDetailsInRunspace(Runspace runspace) { powerShell.Runspace = runspace; powerShell.Commands = command; + var invocationSettings = new PSInvocationSettings(); + invocationSettings.AddToHistory = false; return powerShell - .Invoke() + .Invoke(null, invocationSettings) .FirstOrDefault(); } });