Skip to content

Commit 88c7521

Browse files
committed
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.
1 parent 55f7172 commit 88c7521

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs

+19-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter;
1212
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
13-
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1413
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Runspace;
1514

1615
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging
@@ -54,10 +53,7 @@ public async Task<IReadOnlyList<BreakpointDetails>> SetLineBreakpointsAsync(
5453
? $"Enable-DscDebug -Breakpoint {hashtableString}"
5554
: "Disable-DscDebug");
5655

57-
await executionService.ExecutePSCommandAsync(
58-
dscCommand,
59-
CancellationToken.None)
60-
.ConfigureAwait(false);
56+
await executionService.ExecutePSCommandAsync(dscCommand, CancellationToken.None).ConfigureAwait(false);
6157

6258
// Verify all the breakpoints and return them
6359
foreach (BreakpointDetails breakpoint in breakpoints)
@@ -80,7 +76,7 @@ public bool IsDscResourcePath(string scriptPath)
8076
public static async Task<DscBreakpointCapability> GetDscCapabilityAsync(
8177
ILogger logger,
8278
IRunspaceInfo currentRunspace,
83-
PsesInternalHost psesHost)
79+
IInternalPowerShellExecutionService executionService)
8480
{
8581
// DSC support is enabled only for Windows PowerShell.
8682
if ((currentRunspace.PowerShellVersionDetails.Version.Major >= 6) &&
@@ -92,16 +88,13 @@ public static async Task<DscBreakpointCapability> GetDscCapabilityAsync(
9288
if (!isDscInstalled.HasValue)
9389
{
9490
PSCommand psCommand = new PSCommand()
95-
.AddScript($"$global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference = $ProgressPreference")
96-
.AddScript("$ProgressPreference = 'SilentlyContinue'")
9791
.AddCommand(@"Microsoft.PowerShell.Core\Import-Module")
9892
.AddParameter("Name", "PSDesiredStateConfiguration")
9993
.AddParameter("PassThru")
100-
.AddParameter("ErrorAction", ActionPreference.Ignore)
101-
.AddScript($"$ProgressPreference = $global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference");
94+
.AddParameter("ErrorAction", ActionPreference.Ignore);
10295

10396
IReadOnlyList<PSModuleInfo> dscModule =
104-
await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
97+
await executionService.ExecutePSCommandAsync<PSModuleInfo>(
10598
psCommand,
10699
CancellationToken.None,
107100
new PowerShellExecutionOptions { ThrowOnError = false })
@@ -113,19 +106,29 @@ await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
113106

114107
if (isDscInstalled.Value)
115108
{
116-
PSCommand psCommand = new PSCommand()
117-
.AddCommand("Get-DscResource")
118-
.AddCommand("Select-Object")
119-
.AddParameter("ExpandProperty", "ParentPath");
109+
// Note that __psEditorServices_ is DebugService.PsesGlobalVariableNamePrefix but
110+
// it's notoriously difficult to interpolate it in this script, which has to be a
111+
// single script to guarantee everything is run at once.
112+
PSCommand psCommand = new PSCommand().AddScript(
113+
"""
114+
try {
115+
$global:__psEditorServices_prevProgressPreference = $ProgressPreference
116+
$global:ProgressPreference = 'SilentlyContinue'
117+
return Get-DscResource | Select-Object -ExpandProperty ParentPath
118+
} finally {
119+
$ProgressPreference = $global:__psEditorServices_prevProgressPreference
120+
}
121+
""");
120122

121123
IReadOnlyList<string> resourcePaths =
122-
await psesHost.ExecutePSCommandAsync<string>(
124+
await executionService.ExecutePSCommandAsync<string>(
123125
psCommand,
124126
CancellationToken.None,
125127
new PowerShellExecutionOptions { ThrowOnError = false }
126128
).ConfigureAwait(false);
127129

128130
logger.LogTrace($"DSC resources found: {resourcePaths.Count}");
131+
129132
return new DscBreakpointCapability
130133
{
131134
dscResourceRootPaths = resourcePaths.ToArray()

0 commit comments

Comments
 (0)