diff --git a/PowerShellEditorServices.Common.props b/PowerShellEditorServices.Common.props index 2f07fcc7f..d9679732e 100644 --- a/PowerShellEditorServices.Common.props +++ b/PowerShellEditorServices.Common.props @@ -9,6 +9,5 @@ git https://github.com/PowerShell/PowerShellEditorServices portable - 1.0.3 diff --git a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs index 2199e1839..047944359 100644 --- a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs @@ -6,11 +6,12 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Management.Automation; using System.Management.Automation.Runspaces; namespace Microsoft.PowerShell.EditorServices.Session { + using System.Management.Automation; + internal class PowerShell3Operations : IVersionSpecificOperations { public void ConfigureDebugger(Runspace runspace) @@ -32,27 +33,11 @@ public IEnumerable ExecuteCommandInDebugger( out DebuggerResumeAction? debuggerResumeAction) { IEnumerable executionResult = null; - - using (var nestedPipeline = currentRunspace.CreateNestedPipeline()) + using (var pwsh = PowerShell.Create()) { - foreach (var command in psCommand.Commands) - { - nestedPipeline.Commands.Add(command); - } - - var results = nestedPipeline.Invoke(); - - if (typeof(TResult) != typeof(PSObject)) - { - executionResult = - results - .Select(pso => pso.BaseObject) - .Cast(); - } - else - { - executionResult = results.Cast(); - } + pwsh.Runspace = currentRunspace; + pwsh.Commands = psCommand; + executionResult = pwsh.Invoke(); } // Write the output to the host if necessary diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index f8d6e69a3..5ae22c03e 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -823,44 +823,11 @@ await this.ExecuteCommand( internal static TResult ExecuteScriptAndGetItem(string scriptToExecute, Runspace runspace, TResult defaultValue = default(TResult)) { - Pipeline pipeline = null; - - try + using (var pwsh = PowerShell.Create()) { - if (runspace.RunspaceAvailability == RunspaceAvailability.AvailableForNestedCommand) - { - pipeline = runspace.CreateNestedPipeline(scriptToExecute, false); - } - else - { - pipeline = runspace.CreatePipeline(scriptToExecute, false); - } - - Collection results = pipeline.Invoke(); - - if (results.Count == 0 || results.FirstOrDefault() == null) - { - return defaultValue; - } - - if (typeof(TResult) != typeof(PSObject)) - { - return results - .Select(pso => pso.BaseObject) - .OfType() - .FirstOrDefault(); - } - else - { - return - results - .OfType() - .FirstOrDefault(); - } - } - finally - { - pipeline.Dispose(); + pwsh.Runspace = runspace; + Collection results = pwsh.AddScript(scriptToExecute).Invoke(); + return results.DefaultIfEmpty(defaultValue).First(); } } @@ -1526,6 +1493,12 @@ private async Task GetSessionDetailsInRunspace() } } + private SessionDetails GetSessionDetailsInNestedPipeline() + { + + return GetSessionDetailsInRunspace(this.CurrentRunspace.Runspace); + } + private SessionDetails GetSessionDetailsInRunspace(Runspace runspace) { SessionDetails sessionDetails = @@ -1561,24 +1534,6 @@ private SessionDetails GetSessionDetailsInDebugger() }); } - private SessionDetails GetSessionDetailsInNestedPipeline() - { - using (var pipeline = this.CurrentRunspace.Runspace.CreateNestedPipeline()) - { - return this.GetSessionDetails( - command => - { - pipeline.Commands.Clear(); - pipeline.Commands.Add(command.Commands[0]); - - return - pipeline - .Invoke() - .FirstOrDefault(); - }); - } - } - private void SetProfileVariableInCurrentRunspace(ProfilePaths profilePaths) { // Create the $profile variable