Skip to content

Commit 7ac9fe7

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 611670f commit 7ac9fe7

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

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

+24-12
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)
@@ -77,10 +73,12 @@ public bool IsDscResourcePath(string scriptPath)
7773
StringComparison.CurrentCultureIgnoreCase));
7874
}
7975

76+
private const string prevProgressPreferenceVariable = $"$global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference";
77+
8078
public static async Task<DscBreakpointCapability> GetDscCapabilityAsync(
8179
ILogger logger,
8280
IRunspaceInfo currentRunspace,
83-
PsesInternalHost psesHost)
81+
IInternalPowerShellExecutionService executionService)
8482
{
8583
// DSC support is enabled only for Windows PowerShell.
8684
if ((currentRunspace.PowerShellVersionDetails.Version.Major >= 6) &&
@@ -92,16 +90,13 @@ public static async Task<DscBreakpointCapability> GetDscCapabilityAsync(
9290
if (!isDscInstalled.HasValue)
9391
{
9492
PSCommand psCommand = new PSCommand()
95-
.AddScript($"$global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference = $ProgressPreference")
96-
.AddScript("$ProgressPreference = 'SilentlyContinue'")
9793
.AddCommand(@"Microsoft.PowerShell.Core\Import-Module")
9894
.AddParameter("Name", "PSDesiredStateConfiguration")
9995
.AddParameter("PassThru")
100-
.AddParameter("ErrorAction", ActionPreference.Ignore)
101-
.AddScript($"$ProgressPreference = $global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference");
96+
.AddParameter("ErrorAction", ActionPreference.Ignore);
10297

10398
IReadOnlyList<PSModuleInfo> dscModule =
104-
await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
99+
await executionService.ExecutePSCommandAsync<PSModuleInfo>(
105100
psCommand,
106101
CancellationToken.None,
107102
new PowerShellExecutionOptions { ThrowOnError = false })
@@ -113,19 +108,36 @@ await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
113108

114109
if (isDscInstalled.Value)
115110
{
111+
// Silence the progress output from Get-DscResource.
112+
PSCommand setProgressPreferenceCommand = new PSCommand()
113+
.AddScript($"{prevProgressPreferenceVariable} = $ProgressPreference")
114+
.AddStatement()
115+
.AddScript("$ProgressPreference = 'SilentlyContinue'");
116+
117+
await executionService.ExecutePSCommandAsync(
118+
setProgressPreferenceCommand, CancellationToken.None).ConfigureAwait(false);
119+
116120
PSCommand psCommand = new PSCommand()
117121
.AddCommand("Get-DscResource")
118122
.AddCommand("Select-Object")
119123
.AddParameter("ExpandProperty", "ParentPath");
120124

121125
IReadOnlyList<string> resourcePaths =
122-
await psesHost.ExecutePSCommandAsync<string>(
126+
await executionService.ExecutePSCommandAsync<string>(
123127
psCommand,
124128
CancellationToken.None,
125129
new PowerShellExecutionOptions { ThrowOnError = false }
126130
).ConfigureAwait(false);
127131

128132
logger.LogTrace($"DSC resources found: {resourcePaths.Count}");
133+
134+
// Restore previous progress preference.
135+
PSCommand restoreProgressPreferenceCommand = new PSCommand()
136+
.AddScript($"$ProgressPreference = {prevProgressPreferenceVariable}");
137+
138+
await executionService.ExecutePSCommandAsync(
139+
restoreProgressPreferenceCommand, CancellationToken.None).ConfigureAwait(false);
140+
129141
return new DscBreakpointCapability
130142
{
131143
dscResourceRootPaths = resourcePaths.ToArray()

0 commit comments

Comments
 (0)