Skip to content

Silence progress output of Get-DscResource (take two) #2081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,10 +53,7 @@ public async Task<IReadOnlyList<BreakpointDetails>> 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)
Expand All @@ -80,7 +76,7 @@ public bool IsDscResourcePath(string scriptPath)
public static async Task<DscBreakpointCapability> GetDscCapabilityAsync(
ILogger logger,
IRunspaceInfo currentRunspace,
PsesInternalHost psesHost)
IInternalPowerShellExecutionService executionService)
{
// DSC support is enabled only for Windows PowerShell.
if ((currentRunspace.PowerShellVersionDetails.Version.Major >= 6) &&
Expand All @@ -92,16 +88,13 @@ public static async Task<DscBreakpointCapability> 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<PSModuleInfo> dscModule =
await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
await executionService.ExecutePSCommandAsync<PSModuleInfo>(
psCommand,
CancellationToken.None,
new PowerShellExecutionOptions { ThrowOnError = false })
Expand All @@ -113,19 +106,29 @@ await psesHost.ExecutePSCommandAsync<PSModuleInfo>(

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<string> resourcePaths =
await psesHost.ExecutePSCommandAsync<string>(
await executionService.ExecutePSCommandAsync<string>(
psCommand,
CancellationToken.None,
new PowerShellExecutionOptions { ThrowOnError = false }
).ConfigureAwait(false);

logger.LogTrace($"DSC resources found: {resourcePaths.Count}");

return new DscBreakpointCapability
{
dscResourceRootPaths = resourcePaths.ToArray()
Expand Down