Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/System.Management.Automation/engine/NativeCommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,7 @@ private ProcessStartInfo GetProcessStartInfo(bool redirectOutput, bool redirectE
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = this.Path;

// On Windows, check the extension list and see if we should try to execute this directly.
// Otherwise, use the platform library to check executability
if ((Platform.IsWindows && ValidateExtension(this.Path))
|| (!Platform.IsWindows && Platform.NonWindowsIsExecutable(this.Path)))
if (IsExecutable(this.Path))
{
startInfo.UseShellExecute = false;
if (redirectInput)
Expand Down Expand Up @@ -1288,31 +1285,37 @@ private void CalculateIORedirection(out bool redirectOutput, out bool redirectEr
}
}

private bool ValidateExtension(string path)
// On Windows, check the extension list and see if we should try to execute this directly.
// Otherwise, use the platform library to check executability
private bool IsExecutable(string path)
{
// Now check the extension and see if it's one of the ones in pathext
#if UNIX
return Platform.NonWindowsIsExecutable(this.Path);
#else

string myExtension = System.IO.Path.GetExtension(path);

string pathext = (string)LanguagePrimitives.ConvertTo(
this.Command.Context.GetVariableValue(SpecialVariables.PathExtVarPath),
typeof(string), CultureInfo.InvariantCulture);
var pathext = Environment.GetEnvironmentVariable("PATHEXT");
string[] extensionList;
if (String.IsNullOrEmpty(pathext))
if (string.IsNullOrEmpty(pathext))
{
extensionList = new string[] { ".exe", ".com", ".bat", ".cmd" };
}
else
{
extensionList = pathext.Split(Utils.Separators.Semicolon);
}

foreach (string extension in extensionList)
{
if (String.Equals(extension, myExtension, StringComparison.OrdinalIgnoreCase))
if (string.Equals(extension, myExtension, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
#endif
}

#region Interop for FindExecutable...
Expand Down
4 changes: 2 additions & 2 deletions src/System.Management.Automation/engine/SpecialVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ internal static class SpecialVariables

internal const string EventError = "error";
internal static readonly VariablePath EventErrorVarPath = new VariablePath("script:" + EventError);

#if !UNIX
internal const string PathExt = "env:PATHEXT";
internal static readonly VariablePath PathExtVarPath = new VariablePath(PathExt);

#endif
internal const string PSEmailServer = "PSEmailServer";
internal static readonly VariablePath PSEmailServerVarPath = new VariablePath(PSEmailServer);

Expand Down