I'm trying to write some PowerShell functions that do some stuff and then transparently call through to existing built-in function. I want to pass along all the arguments untouched. I don't want to have to know any details of the arguments.
I tired using 'splat' to do this with @args but that didn't work as I expected.
In the example below, I've written a toy function called myls which supposed to print hello! and then call the same built-in function, Get-ChildItem, that the built-in alias ls calls with the rest of the argument line intact. What I have so far works pretty well:
function myls
{
Write-Output "hello!"
Invoke-Expression("Get-ChildItem "+$MyInvocation.UnboundArguments -join " ")
}
A correct version of myls should be able to handle being called with no arguments, with one argument, with named arguments, from a line containing multiple semi-colon delimited commands, and with variables in the arguments including string variables containing spaces.
The tests below compare myls and the builtin ls:
[NOTE: output elided and/or compacted to save space]
PS> md C:\p\d\x, C:\p\d\y, C:\p\d\"jay z"
PS> cd C:\p\d
PS> ls # no args
PS> myls # pass
PS> cd ..
PS> ls d # one arg
PS> myls d # pass
PS> $a="A"; $z="Z"; $y="y"; $jz="jay z"
PS> $a; ls d; $z # multiple statements
PS> $a; myls d; $z # pass
PS> $a; ls d -Exclude x; $z # named args
PS> $a; myls d -Exclude x; $z # pass
PS> $a; ls d -Exclude $y; $z # variables in arg-line
PS> $a; myls d -Exclude $y; $z # pass
PS> $a; ls d -Exclude $jz; $z # variables containing spaces in arg-line
PS> $a; myls d -Exclude $jz; $z # FAIL!
Is there a way I can re-write myls to get the behavior I want?