I have pulled below example from Microsoft Help docs and experimenting a bit further. I’m able to understand the fact that to pass an array as single object to ScriptBlock param, use unary operator.
How about when I got multiple parameters with [ String ] or [array] in ScriptBlock ?
Eg:
$array = 'Hello','World'
icm -ScriptBlock {param([string[]]$p1) $p1 -join '**'} -Argumentlist (,$array)
Output: Hello**World
$arr1 = 'Hello','World'
$arr2 = 'Welcome','PS'
icm -ScriptBlock {param([string[]]$p1,[string[]]$p2) $p1,$p2 -join '**'} -Argumentlist (,$arr1,$arr2)
Output: System.String[]**System.String[]
icm -ScriptBlock { param([string]$p1, [string]$p2) $p1,$p2 -join '**'} -ArgumentList $arr1, $arr2
Output: Hello World**Welcome PS
3rd attempt, I have removed for string type hoping it would fail but it gave output. Not desired one though !
Expected Output: HelloWorld Welcome**PS
PS C:\WINDOWS\system32>
$arr1 = 'Hello','World'
$arr2 = 'Welcome','PS'
#An array is an object, using the comma is an array of objects or an array of arrays
$arr1,$arr2 -join '**'
#To join the arrays into a single array for join, use a plus + operator
$arr1 + $arr2 -join '**'
System.Object[]**System.Object[]
Hello**World**Welcome**PS
Can you show it using scriptblocks and args list - if possible please …
Main goal is to leverage -ScriptBlock and -ArgumentList parameters of cmdlets to pass multiple arrays as input. No specific to use join operator
Rob has already given you the answer and examples. Simply add the arrays together with + before using join.
$arr1 = 'Hello','World'
$arr2 = 'Welcome','PS'
icm -ScriptBlock { param([string[]]$p1, [string[]]$p2) $p1 + $p2 -join '**'} -ArgumentList $arr1, $arr2
Output
Hello**World**Welcome**PS
[quote quote=242942]Rob has already given you the answer and examples. Simply add the arrays together with + before using join.
$arr1 = ‘Hello’,‘World’ $arr2 = ‘Welcome’,‘PS’ icm -ScriptBlock { param([string ]$p1, [string ]$p2) $p1 + $p2 -join ‘**’} -ArgumentList $arr1, $arr2 Output
HelloWorld Welcome**PS [/quote]
My bad… Thanks for pointing that… Appreciate all
js2010
July 16, 2020, 9:02am
6
Actually, there’s no array problem with multiple arguments.
$a = 1,2,3; $b = 4,5,6
invoke-command localhost { param ($a,$b) $a;$b} -args $a,$b # elevated prompt for localhost
1
2
3
4
5
6
He’s trying to join them, and as arrays it was array1 join array2 where he really wanted each element of each array joined.
js2010
July 16, 2020, 10:27am
8
Oh, I see. This wouldn’t work even outside invoke-command:
$a = 1,2,3; $b = 4,5,6
$a,$b -join '**'
System.Object[]**System.Object[]