0
$cmd = {
    param([System.Array]$filestocopy = $(throw "need files"),
    [bool]$copyxml)
    if($copy)
        #do stuff
}
$files = @("one","two","three")

invoke-command -session $s -scriptblock $cmd -argumentlist (,$files) $copyxml

Error:

Invoke-Command : A positional parameter cannot be found that accepts argument 'True'.

I have searched high and low and cannot find how to pass in an array along with something in a argumentlist. I have tried: (,$files,$copyxml), (,$files),$copyxml, and (,$files) $copyxml

Is there a way to do this?

1 Answer 1

3

The argument to the parameter -ArgumentList must be an array, otherwise $copyxml will be interpreted as the next positional parameter to Invoke-Command. Also, passing the array in a subexpression ((,$files)) will cause it to be mangled. Simply passing the variable ($files) is sufficient. Change this:

invoke-command -session $s -scriptblock $cmd -argumentlist (,$files) $copyxml

into this:

invoke-command -session $s -scriptblock $cmd -argumentlist $files,$copyxml
Sign up to request clarification or add additional context in comments.

4 Comments

Since $files is already an array you shouldn't need the (,$files),$copyxml and should be able to just do $files,$copyxml
So now when I do a foreach($f in $filestocopy), $f = "fileone filetwo filethree". What happened?
@TheMadTechnician You're right. (,$files) doesn't do what the OP wants, because the subexpression mangles the array before passing it into the function.
I thought I had tried $files,$copyxml once, but I guess I didn't. That worked, as expected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.