Pass multiple array values as ArgumentList in ScriptBlock

Welcome Forums General PowerShell Q&A Pass multiple array values as ArgumentList in ScriptBlock

  • This topic has 7 replies, 4 voices, and was last updated 2 months ago by
    js
    Participant
    .
Viewing 7 reply threads
  • Author
    Posts
    • #242894
      Participant
      Topics: 6
      Replies: 12
      Points: 43
      Rank: Member

      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:

      3rd attempt, I have removed [] for string type hoping it would fail but it gave output. Not desired one though !

      Expected Output: <span style="color: #333399">Hello**World**Welcome**PS</span>

      • This topic was modified 2 months ago by Harsha. Reason: Added result
      • This topic was modified 2 months ago by Harsha.
    • #242930
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero
    • #242936
      Participant
      Topics: 6
      Replies: 12
      Points: 43
      Rank: Member

      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

    • #242942
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      Rob has already given you the answer and examples. Simply add the arrays together with + before using join.

      Output

    • #242945
      Participant
      Topics: 6
      Replies: 12
      Points: 43
      Rank: Member

      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

      My bad.. Thanks for pointing that.. Appreciate all 👍

    • #242972
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

      Actually, there’s no array problem with multiple arguments.

      • This reply was modified 2 months ago by js.
    • #242978
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      He’s trying to join them, and as arrays it was array1 join array2 where he really wanted each element of each array joined.

    • #242981
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

      Oh, I see. This wouldn’t work even outside invoke-command:

Viewing 7 reply threads
  • You must be logged in to reply to this topic.