@@ -42,34 +42,25 @@ function ConvertTo-SplatExpression {
4242
4343 $commandName , $elements = $Ast.CommandElements.Where ({ $true }, ' Split' , 1 )
4444
45- $splat = @ {}
46- $retainedArgs = [List [Ast ]]::new()
47- $elementsExtent = $elements.Extent | Join-ScriptExtent
48- $elements = [Queue [ Ast ]]::new( $elements -as [ Ast []])
45+ $splat = @ {}
46+ $retainedArgs = [List [Ast ]]::new()
47+ $elementsExtent = $elements.Extent | Join-ScriptExtent
48+ $boundParameters = [StaticParameterBinder ]::BindCommand( $ Ast).BoundParameters
4949
5050 # Start building the hash table of named parameters and values
51- while ($elements .Count -and ( $current = $elements .Dequeue () )) {
52- if ( $current -isnot [ CommandParameterAst ]) {
53- # We don't try to figure out positional arguments, so we keep them in final CommandAst
54- $retainedArgs.Add ($current )
51+ foreach ($parameter in $boundParameters .GetEnumerator ( )) {
52+ # If the command isn't loaded positional parameters come through as their numeric position.
53+ if ( $parameter .Key -match ' \d+ ' -and -not $parameter .Value.Parameter ) {
54+ $retainedArgs.Add ($parameter .Value.Value )
5555 continue
5656 }
57- # The while is to loop through consecutive switch parameters.
58- while ($current -is [CommandParameterAst ]) {
59- $lastParam = $current
60- if (-not $elements.Count ) {
61- $splat .$lastParam = ' $true'
62- break
63- }
64-
65- $current = $elements.Dequeue ()
66-
67- if ($current -is [CommandParameterAst ]) {
68- $splat .$lastParam = ' $true'
69- } else {
70- $splat .$lastParam = $current
71- }
57+ # The "Value" property for switches is the parameter AST (e.g. -Force) so we need to
58+ # manually build the expression.
59+ if ($parameter.Value.ConstantValue -is [bool ]) {
60+ $splat .($parameter.Key ) = ' ${0}' -f $parameter.Value.ConstantValue.ToString ().ToLower()
61+ continue
7262 }
63+ $splat .($parameter.Key ) = $parameter.Value.Value
7364 }
7465
7566 # Remove the hypen, change to camelCase and add 'Splat'
@@ -88,7 +79,7 @@ function ConvertTo-SplatExpression {
8879 $null = & {
8980 foreach ($pair in $splat.GetEnumerator ()) {
9081 $sb.Append (' ' ).
91- Append($pair.Key.ParameterName ).
82+ Append($pair.Key ).
9283 Append(' = ' )
9384 if ($pair.Value -is [ArrayLiteralAst ]) {
9485 $sb.AppendLine ($pair.Value.Elements.ForEach {
0 commit comments