15
15
16
16
namespace Microsoft.PowerShell.EditorServices
17
17
{
18
+ using System.Collections;
18
19
using System.Management.Automation;
19
20
using System.Management.Automation.Host;
20
21
using System.Management.Automation.Runspaces;
@@ -68,6 +69,14 @@ public PowerShellContextState SessionState
68
69
private set;
69
70
}
70
71
72
+ /// <summary>
73
+ /// PowerShell Version of the current runspace.
74
+ /// </summary>
75
+ public Version PowerShellVersion
76
+ {
77
+ get; private set;
78
+ }
79
+
71
80
#endregion
72
81
73
82
#region Constructors
@@ -109,7 +118,7 @@ private void Initialize(Runspace initialRunspace)
109
118
110
119
this.initialRunspace = initialRunspace;
111
120
this.currentRunspace = initialRunspace;
112
- this.currentRunspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
121
+
113
122
this.currentRunspace.Debugger.BreakpointUpdated += OnBreakpointUpdated;
114
123
this.currentRunspace.Debugger.DebuggerStop += OnDebuggerStop;
115
124
@@ -120,9 +129,38 @@ private void Initialize(Runspace initialRunspace)
120
129
// TODO: Should this be configurable?
121
130
this.SetExecutionPolicy(ExecutionPolicy.RemoteSigned);
122
131
132
+ PowerShellVersion = GetPowerShellVersion();
133
+
134
+ #if !PowerShellv3
135
+ if (PowerShellVersion > new Version(3,0))
136
+ {
137
+ this.currentRunspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
138
+ }
139
+ #endif
140
+
123
141
this.SessionState = PowerShellContextState.Ready;
124
142
}
125
143
144
+ private Version GetPowerShellVersion()
145
+ {
146
+ try
147
+ {
148
+ var psVersionTable = this.currentRunspace.SessionStateProxy.GetVariable("PSVersionTable") as Hashtable;
149
+ if (psVersionTable != null)
150
+ {
151
+ var version = psVersionTable["PSVersion"] as Version;
152
+ if (version == null) return new Version(5, 0);
153
+ return version;
154
+ }
155
+ }
156
+ catch (Exception ex)
157
+ {
158
+ Logger.Write(LogLevel.Warning, "Failed to look up PowerShell version. Defaulting to version 5. " + ex.Message);
159
+ }
160
+
161
+ return new Version(5, 0);
162
+ }
163
+
126
164
#endregion
127
165
128
166
#region Public Methods
@@ -370,7 +408,12 @@ internal void BreakExecution()
370
408
{
371
409
Logger.Write(LogLevel.Verbose, "Debugger break requested...");
372
410
373
- this.currentRunspace.Debugger.SetDebuggerStepMode(true);
411
+ #if PowerShellv5
412
+ if (PowerShellVersion >= new Version(5, 0))
413
+ {
414
+ this.currentRunspace.Debugger.SetDebuggerStepMode(true);
415
+ }
416
+ #endif
374
417
}
375
418
376
419
internal void ResumeDebugger(DebuggerResumeAction resumeAction)
@@ -568,7 +611,7 @@ private static string GetStringForPSCommand(PSCommand psCommand)
568
611
569
612
return stringBuilder.ToString();
570
613
}
571
-
614
+
572
615
private void SetExecutionPolicy(ExecutionPolicy desiredExecutionPolicy)
573
616
{
574
617
var currentPolicy = ExecutionPolicy.Undefined;
0 commit comments