Fix the ssh PATH check in SSHConnectionInfo when the default Runspace is not available (#25780)

This commit is contained in:
Jordan Borean
2025-08-04 16:37:45 -07:00
committed by GitHub
parent 65b899c3a9
commit 8dd0d2acf8
@@ -2206,12 +2206,54 @@ namespace System.Management.Automation.Runspaces
var context = Runspaces.LocalPipeline.GetExecutionContextFromTLS();
if (context != null)
{
var cmdInfo = context.CommandDiscovery.LookupCommandInfo(sshCommand, CommandOrigin.Internal) as ApplicationInfo;
var cmdInfo = CommandDiscovery.LookupCommandInfo(
sshCommand,
CommandTypes.Application,
SearchResolutionOptions.None,
CommandOrigin.Internal,
context) as ApplicationInfo;
if (cmdInfo != null)
{
filePath = cmdInfo.Path;
}
}
else
{
// A Runspace may not be present in the TLS in SDK hosted apps
// or if running in another thread without a Runspace. While
// 'ProcessStartInfo' can lookup the full path in PATH, it searches
// the process' working directory first. 'LookupCommandInfo' does
// not search the process' working directory and we want to keep that
// behavior. We also get the parent dir of the full path to set as the
// new WorkingDirectory. So, we do a manual lookup here only in PATH.
string[] entries = Environment.GetEnvironmentVariable("PATH")?.Split(
Path.PathSeparator,
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) ?? [];
foreach (var path in entries)
{
if (!Path.IsPathFullyQualified(path))
{
continue;
}
var sshCommandPath = Path.Combine(path, sshCommand);
if (File.Exists(sshCommandPath))
{
filePath = sshCommandPath;
break;
}
}
}
if (string.IsNullOrEmpty(filePath))
{
throw new CommandNotFoundException(
sshCommand,
null,
"CommandNotFoundException",
DiscoveryExceptions.CommandNotFoundException);
}
// Create a local ssh process (client) that conects to a remote sshd process (server) using a 'powershell' subsystem.
//