From 88c752169970d2f409168e8ff24e43548577e8f2 Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:56:36 -0700 Subject: [PATCH] Silence progress output of `Get-DscResource` (take two) It wasn't the module import that was being noisy but the next command. It sure looked like the last commit "fixed" it in testing because the output was certainly silenced...but that was actually due to the side effect of having added an `AddScript` to the `PSCommand` causing it to no longer return the imported module at all. Without that return value, the noisy code wasn't executing either. Now the progress preference is saved, set to silent, `Get-DscResource` is run (silently) and then the preference is restored. Double-checked in the debugger. --- .../Debugging/DscBreakpointCapability.cs | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs b/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs index c929f4bcc..763094be4 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs @@ -10,7 +10,6 @@ using Microsoft.Extensions.Logging; using Microsoft.PowerShell.EditorServices.Services.DebugAdapter; using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution; -using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host; using Microsoft.PowerShell.EditorServices.Services.PowerShell.Runspace; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging @@ -54,10 +53,7 @@ public async Task> SetLineBreakpointsAsync( ? $"Enable-DscDebug -Breakpoint {hashtableString}" : "Disable-DscDebug"); - await executionService.ExecutePSCommandAsync( - dscCommand, - CancellationToken.None) - .ConfigureAwait(false); + await executionService.ExecutePSCommandAsync(dscCommand, CancellationToken.None).ConfigureAwait(false); // Verify all the breakpoints and return them foreach (BreakpointDetails breakpoint in breakpoints) @@ -80,7 +76,7 @@ public bool IsDscResourcePath(string scriptPath) public static async Task GetDscCapabilityAsync( ILogger logger, IRunspaceInfo currentRunspace, - PsesInternalHost psesHost) + IInternalPowerShellExecutionService executionService) { // DSC support is enabled only for Windows PowerShell. if ((currentRunspace.PowerShellVersionDetails.Version.Major >= 6) && @@ -92,16 +88,13 @@ public static async Task GetDscCapabilityAsync( if (!isDscInstalled.HasValue) { PSCommand psCommand = new PSCommand() - .AddScript($"$global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference = $ProgressPreference") - .AddScript("$ProgressPreference = 'SilentlyContinue'") .AddCommand(@"Microsoft.PowerShell.Core\Import-Module") .AddParameter("Name", "PSDesiredStateConfiguration") .AddParameter("PassThru") - .AddParameter("ErrorAction", ActionPreference.Ignore) - .AddScript($"$ProgressPreference = $global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference"); + .AddParameter("ErrorAction", ActionPreference.Ignore); IReadOnlyList dscModule = - await psesHost.ExecutePSCommandAsync( + await executionService.ExecutePSCommandAsync( psCommand, CancellationToken.None, new PowerShellExecutionOptions { ThrowOnError = false }) @@ -113,19 +106,29 @@ await psesHost.ExecutePSCommandAsync( if (isDscInstalled.Value) { - PSCommand psCommand = new PSCommand() - .AddCommand("Get-DscResource") - .AddCommand("Select-Object") - .AddParameter("ExpandProperty", "ParentPath"); + // Note that __psEditorServices_ is DebugService.PsesGlobalVariableNamePrefix but + // it's notoriously difficult to interpolate it in this script, which has to be a + // single script to guarantee everything is run at once. + PSCommand psCommand = new PSCommand().AddScript( + """ + try { + $global:__psEditorServices_prevProgressPreference = $ProgressPreference + $global:ProgressPreference = 'SilentlyContinue' + return Get-DscResource | Select-Object -ExpandProperty ParentPath + } finally { + $ProgressPreference = $global:__psEditorServices_prevProgressPreference + } + """); IReadOnlyList resourcePaths = - await psesHost.ExecutePSCommandAsync( + await executionService.ExecutePSCommandAsync( psCommand, CancellationToken.None, new PowerShellExecutionOptions { ThrowOnError = false } ).ConfigureAwait(false); logger.LogTrace($"DSC resources found: {resourcePaths.Count}"); + return new DscBreakpointCapability { dscResourceRootPaths = resourcePaths.ToArray()