diff --git a/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs b/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs index 59715a02125..200cfeee2ef 100644 --- a/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs +++ b/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs @@ -314,7 +314,7 @@ private void AppendOneNativeArgument(ExecutionContext context, CommandParameterI } else { - if (argArrayAst != null && ArgumentPassingStyle == NativeArgumentPassingStyle.Standard) + if (argArrayAst != null && ArgumentPassingStyle != NativeArgumentPassingStyle.Legacy) { // We have a literal array, so take the extent, break it on spaces and add them to the argument list. foreach (string element in argArrayAst.Extent.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries)) @@ -331,7 +331,7 @@ private void AppendOneNativeArgument(ExecutionContext context, CommandParameterI } } } - else if (ArgumentPassingStyle == NativeArgumentPassingStyle.Standard && currentObj != null) + else if (ArgumentPassingStyle != NativeArgumentPassingStyle.Legacy && currentObj != null) { // add empty strings to arglist, but not nulls AddToArgumentList(parameter, arg); diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandArguments.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandArguments.Tests.ps1 index 0c7b7f9d512..c8e3c75cb17 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandArguments.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandArguments.Tests.ps1 @@ -171,7 +171,7 @@ Describe "Will error correctly if an attempt to set variable to improper value" } } -foreach ( $argumentListValue in "Standard","Legacy" ) { +foreach ( $argumentListValue in "Standard","Legacy","Windows" ) { $PSNativeCommandArgumentPassing = $argumentListValue Describe "Native Command Arguments (${PSNativeCommandArgumentPassing})" -tags "CI" { # When passing arguments to native commands, quoted segments that contain @@ -263,6 +263,28 @@ foreach ( $argumentListValue in "Standard","Legacy" ) { $lines[$i] | Should -BeExactly "Arg $i is <$($expected[$i])>" } } + + It "Should handle empty args correctly (ArgumentList=${PSNativeCommandArgumentPassing})" { + if ($PSNativeCommandArgumentPassing -eq 'Legacy') { + $expectedLines = 2 + } + else { + $expectedLines = 3 + } + + $lines = testexe -echoargs 1 '' 2 + $lines.Count | Should -Be $expectedLines + $lines[0] | Should -BeExactly 'Arg 0 is <1>' + + if ($expectedLines -eq 2) { + $lines[1] | Should -BeExactly 'Arg 1 is <2>' + } + else { + $lines[1] | Should -BeExactly 'Arg 1 is <>' + $lines[2] | Should -BeExactly 'Arg 2 is <2>' + } + + } } } Describe 'PSPath to native commands' -tags "CI" {