Both python and powershell support a form of splatting array and named arguments as function inputs, a very useful feature.
However powershell seem to be internally inconsistent somewhat. I am trying to reproduce powershell code that behaves similarly to the following python code:
def echoName(Name, *args, **kwargs): print("Name is:", Name) def wrapper(*args, **kwargs): print("args:", args) print("kwargs:", kwargs) echoName(*args, **kwargs) d = { "Name": "John", "Age": 25 } wrapper(**d) # args: () # kwargs: {'Name': 'John', 'Age': 25} # Name is: John As far as I am aware ValueFromRemainingArguments is the only way to accept left over parameters in a powershell advanced function
function echoName { param( [CmdletBinding()] [string]$Name, [parameter(Mandatory = $False, ValueFromRemainingArguments = $True)] [Object[]] $Arguments ) Write-Host "Name is: $Name" } function wrapper { [CmdletBinding()] param( [parameter(Mandatory = $False, ValueFromRemainingArguments = $True)] [Object[]] $Arguments ) Write-Host "Arguments is: $Arguments" echoName @Arguments } $d = @{ Name = 'John' Age = 25 } wrapper @d # Arguments is: -Name: John -Age: 25 # Name is: -Name: I have 3 issues with powershell's output
- Arguments is now an array
- the named arguments were prefixed with
-and suffixed with: - this is a weird behavior at best:
$a = @(1,2,3) wrapper @a @d # Arguments is: 1 2 3 -Name: John -Age: 25 # Name is: 1 How can I chain and only partially consume variables as possible in python?
What is the difference between a cmdlet and a function?
Wrapper function for cmdlet - pass remaining parameters
Is there a way to create an alias to a cmdlet in a way that it only runs if arguments are passed to the alias?

function wrapper { Write-Host "Args are '$args'";echoName @args }. Removing the param block and CmdletBinding attribute will cause PowerShell to start passing the argument values as-is, without attempting any validation or type coercion during binding.echoName @PSBoundParametersfrom the wrapper, or 2) accept a dictionary instance and splat that, eg.param([System.Collections.IDictionary]$kwArgs) echoName @kwArgs(original call needs to bewrapper $dictinstead ofwrapper @dictthen)