Skip to content

Commit 8b4c978

Browse files
committed
Resolve remote runspace InstanceID correctly
This change improves how we access a remote runspace's InstanceID so that we don't incorrectly identify remote process runspace attach events because we've used the InstanceId from the local RemoteRunspace object instead of the $host.Runspace.InstanceId from the remote session.
1 parent b9e0020 commit 8b4c978

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/PowerShellEditorServices/Session/RunspaceDetails.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,41 @@ public RunspaceDetails(
104104
PowerShellVersionDetails powerShellVersion,
105105
RunspaceLocation runspaceLocation,
106106
string connectionString)
107+
: this(
108+
runspace.InstanceId,
109+
runspace,
110+
powerShellVersion,
111+
runspaceLocation,
112+
connectionString)
107113
{
108-
this.Id = runspace.InstanceId;
114+
}
115+
116+
/// <summary>
117+
/// Creates a new instance of the RunspaceDetails class.
118+
/// </summary>
119+
/// <param name="instanceId">
120+
/// The InstanceId Guid for the runspace.
121+
/// </param>
122+
/// <param name="runspace">
123+
/// The runspace for which this instance contains details.
124+
/// </param>
125+
/// <param name="powerShellVersion">
126+
/// The PowerShellVersionDetails of the runspace.
127+
/// </param>
128+
/// <param name="runspaceLocation">
129+
/// The RunspaceLocale of the runspace.
130+
/// </param>
131+
/// <param name="connectionString">
132+
/// The connection string of the runspace.
133+
/// </param>
134+
public RunspaceDetails(
135+
Guid instanceId,
136+
Runspace runspace,
137+
PowerShellVersionDetails powerShellVersion,
138+
RunspaceLocation runspaceLocation,
139+
string connectionString)
140+
{
141+
this.Id = instanceId;
109142
this.Runspace = runspace;
110143
this.PowerShellVersion = powerShellVersion;
111144
this.Location = runspaceLocation;
@@ -125,6 +158,7 @@ public static RunspaceDetails Create(Runspace runspace)
125158
{
126159
Validate.IsNotNull(nameof(runspace), runspace);
127160

161+
var runspaceId = runspace.InstanceId;
128162
var runspaceLocation = RunspaceLocation.Local;
129163
var versionDetails = PowerShellVersionDetails.GetVersionDetails(runspace);
130164

@@ -150,6 +184,11 @@ public static RunspaceDetails Create(Runspace runspace)
150184

151185
if (runspace.ConnectionInfo.ComputerName != "localhost")
152186
{
187+
runspaceId =
188+
PowerShellContext.ExecuteScriptAndGetItem<Guid>(
189+
"$host.Runspace.InstanceId",
190+
runspace);
191+
153192
runspaceLocation = RunspaceLocation.Remote;
154193
connectionString =
155194
runspace.ConnectionInfo.ComputerName +
@@ -159,6 +198,7 @@ public static RunspaceDetails Create(Runspace runspace)
159198

160199
return
161200
new RunspaceDetails(
201+
runspaceId,
162202
runspace,
163203
versionDetails,
164204
runspaceLocation,
@@ -185,6 +225,7 @@ public static RunspaceDetails CreateAttached(
185225
{
186226
RunspaceDetails newRunspace =
187227
new RunspaceDetails(
228+
attachedRunspaceId,
188229
runspaceDetails.Runspace,
189230
runspaceDetails.PowerShellVersion,
190231
runspaceDetails.Location,

0 commit comments

Comments
 (0)