From 6fa1e86f2d60975595c563f923b5633c1341a424 Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Fri, 15 Jun 2018 11:06:38 -0700 Subject: [PATCH] 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. --- .../remoting/common/RunspaceConnectionInfo.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index 72b2f7c534..3109cc966b 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -2182,7 +2182,7 @@ namespace System.Management.Automation.Runspaces string filename = startInfo.FileName; string[] argv = ParseArgv(startInfo); - string[] envp = new string[0]; + string[] envp = CopyEnvVariables(startInfo); string cwd = !string.IsNullOrWhiteSpace(startInfo.WorkingDirectory) ? startInfo.WorkingDirectory : null; // Invoke the shim fork/execve routine. It will create pipes for all requested @@ -2239,6 +2239,20 @@ namespace System.Management.Automation.Runspaces access, StreamBufferSize, isAsync: false); } + /// Copies environment variables from ProcessStartInfo + /// ProcessStartInfo + /// String array of environment key/value pairs + private static string[] CopyEnvVariables(ProcessStartInfo psi) + { + var envp = new string[psi.Environment.Count]; + int index = 0; + foreach (var pair in psi.Environment) + { + envp[index++] = pair.Key + "=" + pair.Value; + } + return envp; + } + /// Converts the filename and arguments information from a ProcessStartInfo into an argv array. /// The ProcessStartInfo. /// The argv array.