Skip to content

Commit 6fa1e86

Browse files
PaulHiginTravisEz13
authored andcommitted
Added copy env vars from ProcessStartInfo to key/pair array used in creating ssh process (#7070)
This addresses issue #6207. During SSH remoting on Linux machines, when PowerShell on the client creates the SSH process to connect to the target machine, it was not passing in environment variables from the parent process, and this was preventing ssh-agent key management from working. This change adds a helper function to create environment variable key/value pairs for the created SSH process, based on the passed in ProcessStartInfo object.
1 parent 8805ca2 commit 6fa1e86

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,7 @@ internal static int StartSSHProcess(
21822182

21832183
string filename = startInfo.FileName;
21842184
string[] argv = ParseArgv(startInfo);
2185-
string[] envp = new string[0];
2185+
string[] envp = CopyEnvVariables(startInfo);
21862186
string cwd = !string.IsNullOrWhiteSpace(startInfo.WorkingDirectory) ? startInfo.WorkingDirectory : null;
21872187

21882188
// Invoke the shim fork/execve routine. It will create pipes for all requested
@@ -2239,6 +2239,20 @@ private static FileStream OpenStream(int fd, FileAccess access)
22392239
access, StreamBufferSize, isAsync: false);
22402240
}
22412241

2242+
/// <summary>Copies environment variables from ProcessStartInfo </summary>
2243+
/// <param name="psi">ProcessStartInfo</param>
2244+
/// <returns>String array of environment key/value pairs</returns>
2245+
private static string[] CopyEnvVariables(ProcessStartInfo psi)
2246+
{
2247+
var envp = new string[psi.Environment.Count];
2248+
int index = 0;
2249+
foreach (var pair in psi.Environment)
2250+
{
2251+
envp[index++] = pair.Key + "=" + pair.Value;
2252+
}
2253+
return envp;
2254+
}
2255+
22422256
/// <summary>Converts the filename and arguments information from a ProcessStartInfo into an argv array.</summary>
22432257
/// <param name="psi">The ProcessStartInfo.</param>
22442258
/// <returns>The argv array.</returns>

0 commit comments

Comments
 (0)