10
10
using Microsoft . Extensions . Logging ;
11
11
using Microsoft . PowerShell . EditorServices . Services . DebugAdapter ;
12
12
using Microsoft . PowerShell . EditorServices . Services . PowerShell . Execution ;
13
- using Microsoft . PowerShell . EditorServices . Services . PowerShell . Host ;
14
13
using Microsoft . PowerShell . EditorServices . Services . PowerShell . Runspace ;
15
14
16
15
namespace Microsoft . PowerShell . EditorServices . Services . PowerShell . Debugging
@@ -54,10 +53,7 @@ public async Task<IReadOnlyList<BreakpointDetails>> SetLineBreakpointsAsync(
54
53
? $ "Enable-DscDebug -Breakpoint { hashtableString } "
55
54
: "Disable-DscDebug" ) ;
56
55
57
- await executionService . ExecutePSCommandAsync (
58
- dscCommand ,
59
- CancellationToken . None )
60
- . ConfigureAwait ( false ) ;
56
+ await executionService . ExecutePSCommandAsync ( dscCommand , CancellationToken . None ) . ConfigureAwait ( false ) ;
61
57
62
58
// Verify all the breakpoints and return them
63
59
foreach ( BreakpointDetails breakpoint in breakpoints )
@@ -80,7 +76,7 @@ public bool IsDscResourcePath(string scriptPath)
80
76
public static async Task < DscBreakpointCapability > GetDscCapabilityAsync (
81
77
ILogger logger ,
82
78
IRunspaceInfo currentRunspace ,
83
- PsesInternalHost psesHost )
79
+ IInternalPowerShellExecutionService executionService )
84
80
{
85
81
// DSC support is enabled only for Windows PowerShell.
86
82
if ( ( currentRunspace . PowerShellVersionDetails . Version . Major >= 6 ) &&
@@ -92,16 +88,13 @@ public static async Task<DscBreakpointCapability> GetDscCapabilityAsync(
92
88
if ( ! isDscInstalled . HasValue )
93
89
{
94
90
PSCommand psCommand = new PSCommand ( )
95
- . AddScript ( $ "$global:{ DebugService . PsesGlobalVariableNamePrefix } prevProgressPreference = $ProgressPreference")
96
- . AddScript ( "$ProgressPreference = 'SilentlyContinue'" )
97
91
. AddCommand ( @"Microsoft.PowerShell.Core\Import-Module" )
98
92
. AddParameter ( "Name" , "PSDesiredStateConfiguration" )
99
93
. AddParameter ( "PassThru" )
100
- . AddParameter ( "ErrorAction" , ActionPreference . Ignore )
101
- . AddScript ( $ "$ProgressPreference = $global:{ DebugService . PsesGlobalVariableNamePrefix } prevProgressPreference") ;
94
+ . AddParameter ( "ErrorAction" , ActionPreference . Ignore ) ;
102
95
103
96
IReadOnlyList < PSModuleInfo > dscModule =
104
- await psesHost . ExecutePSCommandAsync < PSModuleInfo > (
97
+ await executionService . ExecutePSCommandAsync < PSModuleInfo > (
105
98
psCommand ,
106
99
CancellationToken . None ,
107
100
new PowerShellExecutionOptions { ThrowOnError = false } )
@@ -113,19 +106,29 @@ await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
113
106
114
107
if ( isDscInstalled . Value )
115
108
{
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
+ """ ) ;
120
122
121
123
IReadOnlyList < string > resourcePaths =
122
- await psesHost . ExecutePSCommandAsync < string > (
124
+ await executionService . ExecutePSCommandAsync < string > (
123
125
psCommand ,
124
126
CancellationToken . None ,
125
127
new PowerShellExecutionOptions { ThrowOnError = false }
126
128
) . ConfigureAwait ( false ) ;
127
129
128
130
logger . LogTrace ( $ "DSC resources found: { resourcePaths . Count } ") ;
131
+
129
132
return new DscBreakpointCapability
130
133
{
131
134
dscResourceRootPaths = resourcePaths . ToArray ( )
0 commit comments