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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
stewart99
  • 14,024
  • 7
  • 27
  • 42

1 Answers1

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    Since `$files` is already an array you shouldn't need the `(,$files),$copyxml` and should be able to just do `$files,$copyxml` – TheMadTechnician Mar 20 '14 at 17:01
  • So now when I do a foreach($f in $filestocopy), $f = "fileone filetwo filethree". What happened? – stewart99 Mar 20 '14 at 17:19
  • @TheMadTechnician You're right. `(,$files)` doesn't do what the OP wants, because the subexpression mangles the array before passing it into the function. – Ansgar Wiechers Mar 20 '14 at 18:57
  • I thought I had tried `$files,$copyxml` once, but I guess I didn't. That worked, as expected. – stewart99 Mar 20 '14 at 20:36