Pass an array contained in a variable to a scriptblock using ArgumentList

rated by 0 users
This post has 2 Replies | 2 Followers

Not Ranked
Posts 2
Etzeitet Posted: 06-04-2010 8:07 PM


Invoke-Command -ScriptBlock { param([string]$item) $item } -ArgumentList "Hello"

Run the above and "Hello" will be displayed.

$somestring = "Hello again!"
Invoke-Command -ScriptBlock { param([string]$item) $item } -ArgumentList ` $somestring

This displays "Hello again!" when run.


Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList `
@("Hello", "World")

Displays only "Hello".  Oops!

Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList `
@(,@("Hello", "World"))



See, we're getting somewhere.  Now, how would I pass an array as a variable?

Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList `

That behaves the same was as example 3 - i.e. it only passes the first element in the array.  Example 4 forces to stay as an array [as opposed to splatting?].  How can I do the same when the array is contained in a variable?

I have tried various combinations and nothing seems to work.  So, as the title, how does one pass an array contained in a variable to a scriptblock using ArgumentList?


Top 25 Contributor
Posts 562
Microsoft MVP
Top Contributor

Have you tried:

Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList (,$anArray)




Not Ranked
Posts 2

This did the trick!  Thank you.  One of the things I didn't try.


Page 1 of 1 (3 items) | RSS
Copyright 2012 All rights reserved.