1:
Invoke-Command -ScriptBlock { param([string]$item) $item } -ArgumentList "Hello"
Run the above and "Hello" will be displayed.
2:$somestring = "Hello again!"Invoke-Command -ScriptBlock { param([string]$item) $item } -ArgumentList ` $somestring
This displays "Hello again!" when run.
3:
Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList `@("Hello", "World")
Displays only "Hello". Oops!4:Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList ` @(,@("Hello", "World"))Displays
"HelloWorld"
See, we're getting somewhere. Now, how would I pass an array as a variable?Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList `$anArrayThat 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?
Have you tried:
Invoke-Command -ScriptBlock { param([array]$item) $item } -ArgumentList (,$anArray)
This did the trick! Thank you. One of the things I didn't try.
:)