6

I'm trying to add multiple csv-imported lists to a remote executed PS script. It works fine when I pass only one list using the following command:

Invoke-Command -filepath $createSAPSPath -ConnectionUri $uri -Credential $credential -ArgumentList (,$accountsList)

What would be the correct syntax for multiple list objects? I've tried:

Invoke-Command -filepath $createSAPSPath -ConnectionUri $uri -Credential $credential -ArgumentList (,$accountsList),(,$groupsList)

But it doesn't seem to work...

Thanks,

Glenn

Glenn
  • 489
  • 2
  • 7
  • 19

3 Answers3

11

You have to realize why you need unary comma in the first situation, to understand why its not needed in the second one.

Parameter -ArgumentList takes Object[] argument type. If you are passing single collection, you need to prevent PowerShell from treating single argument (that happens to be a collection) as collection of arguments passed to this parameter.

If you pass something that is collection already (e.g. $AnyObject, $EvenCollection), regardless of the type of individual objects, PowerShell will do what users usually expect: pass first collection to first parameter, second collection to the second parameter.

To sum it up: you should be able to run this like that:

Invoke-Command -filepath $createSAPSPath -ConnectionUri $uri -Credential $credential -ArgumentList $accountsList, $groupList

... and get expected results.

BartekB
  • 8,492
  • 32
  • 33
4

Try it this way:

$AccountList = 'Account1','Account2'
$GroupList    = 'Group1','Group2'

invoke-command {$args[0];'*****';$args[1]} -ArgumentList (,$AccountList,$GroupList) 

Account1
Account2
*****
Group1
Group2
mjolinor
  • 66,130
  • 7
  • 114
  • 135
0

Another one...

$myArrList = [System.Collections.ArrayList]@()
$myArrList2 = [System.Collections.ArrayList]@()

//add your elements here

$arguments = @()
$arguments += (,$myArrList)
$arguments += (,$myArrList2)

$j = Start-Job -Name $('job-' +$env:COMPUTERNAME) -ScriptBlock $scriptBlockJob -ArgumentList $arguments