0

I'm trying to pass unparsed arguments "through" to a function call, but am having trouble getting powershell to parse them. See below code:

[CmdletBinding(PositionalBinding=$false)] 
param([Parameter(Mandatory=$false)] [Alias("m", "mixno", "mix")] [int]$script:mix = 2,               
      [Parameter(Mandatory=$false)] [Alias("c", "command")] [String]$script:command = "help",
      [Parameter(ValueFromRemainingArguments=$true)] [string[]]$script:otherargs)

function f1{
    param([switch]$s1)
    Write-host "Unbound Args: $($MyInvocation.UnboundArguments)"
    Write-Host "s1: $s1"
}

&"$script:command" @script:otherargs

Result:

.\argstest.ps1 -c f1 -s1

Unbound Args: -s1

s1: False
3
  • Does this answer your question? How to pass named parameters to another script in powershell Commented Nov 8, 2019 at 18:44
  • So I have to build a hash table? Seems like there should be an easier way to do it. Commented Nov 11, 2019 at 15:32
  • if you want to pass named parameters: yes. In case you want to pass positional parameters (which is tricky as you might have bound parameters as well), you might simply build an array of the unbound parameters. Commented Nov 11, 2019 at 15:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.