diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index c4c7c106355..7ce7ec20f16 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -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) @@ -1288,16 +1285,19 @@ 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" }; } @@ -1305,14 +1305,17 @@ private bool ValidateExtension(string path) { 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... diff --git a/src/System.Management.Automation/engine/SpecialVariables.cs b/src/System.Management.Automation/engine/SpecialVariables.cs index c95689e5ba7..7d401f33758 100644 --- a/src/System.Management.Automation/engine/SpecialVariables.cs +++ b/src/System.Management.Automation/engine/SpecialVariables.cs @@ -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);