Azure-pipelines-tasks: How to pass an array of strings to a PowerShell task?

Created on 22 Jun 2016  路  10Comments  路  Source: microsoft/azure-pipelines-tasks

Are there any examples of passing an array of strings to a PowerShell task, with visible source? It appears that inputs to tasks are always passed as a single string, and marking them as an array in the PowerShell parameter list always winds up with only one value in the array, the complete list including the commas.

I'd like to know the best/easiest way to accomplish this in my own custom tasks. I've looked through the source here a ton, but the few places I found that look like they accept an "array" take it in as a plain string, and then usually call some magic helper function where the source is not available and its unclear how to use that same function in my tasks.

Help would be appreciated.

Most helpful comment

Thread revival level = 100

Any news here? Discovering that my variable values (e.g. "text,one,two,three,four") is getting passed through as either:

  • A single space-delimited string, the commas removed, or;
  • null

Currently going through the motions of trying to escape/not-escape/single-quote-enclose etc. to figure out how to get this working.

All 10 comments

@brettjacobson, If you are using the PowerShell Script task, you can pass arrays. Try out these inputs:
Type: Inline Script
Arguments: @( 'this is $args[0][0]', 'this is $args[0][1]' ) 'this is $args[1]'
Inline Script:

for ($i = 0 ; $i -lt $args.length ; $i++) {
  $arg = $args[$i]
  if ($arg -is [array]) {
    for ($j = 0 ; $j -lt $arg.length ; $j++) {
      $a = $arg[$j]
      write-host "`$args[$i][$j]: '$a'"
    }
  } else {
    write-host "`$args[$i]: '$arg'"
  }
}

@ericsciple Thanks, but I actually meant in my custom task that I'm implementing, via a PowerShell file. It seems that even if I define my input arguments in the .ps1 with [[string[]]$ComputerName since there is only a string type for the input in the task.json, my parameters in that input are getting passed as a single string (I think TFS "quotes" my argument but I'm not certain) so that I only ever receive a single string in $Computername, and it still has the spaces or the commas that I put in. I can invoke my script directly in a PowerShell console, but when TFS build uses my task, the input "array' is getting collapsed down to a single string, it doesn't show up in my script as an array of strings. I think your info is talking about the PowerShell build task specifically.
I've tried putting the @() around my input parameter in the UI, but it still shows up as a single string (in my custom Task executing with Powershell)

@brettjacobson Oh I see. Sorry, in haste I misunderstood the question.

Regarding

and then usually call some magic helper function where the source is not available and its unclear how to use that same function in my tasks.

Those tasks likely use the deprecated closed source PowerShell task execution handler. We are working on porting the in-the-box tasks away from that execution handler. It will continue to function as is so the tasks don't suddenly break.

New tasks should use the Node execution handler or PowerShell3 execution handler. Docs are here: https://github.com/Microsoft/vsts-task-lib

Today there is no array. You would need to Invoke-Expression or ConvertFrom-Json to deserialize the string.

@chrisrpatterson and @bryanmacfarlane would likely be interested to hear your scenario. There has been discussion of adding a dictionary input. So an array input might be something else to consider.

@ericsciple Thanks for the info. I've currently made my own hacky helper function that splits a string on commas and trims spaces, apostrophes and quotes. I'll continue to use that for now.
Thanks for the link to the task lib. I assume by your comment that Powershell3 is the preferred execution handler? I'm pretty sure I've read all the docs available, and nothing described the difference between Powershell and Powershell3. And almost all of the tasks in this repo are not 3 (I only found 5 that mention Powershell3). I appreciate your help!

@chrisrpatterson @bryanmacfarlane For my use case, I have task inputs that are computer names, and I would like them to be passed treated as an arrary so that they are passed in as an array of strings into my .ps1 task file, instead of the current way where the input is transferred as a single string (even if I have the entry comma delimited). So currently I have to have a custom function to split the string back out into an array, and Trim() spaces and quotes.

In the typescript/node lib, we have a lib function to getDelimitedInputs which splits/trims on char so you don't need a private helper function. we could add the same to ps lib ...

That looks like it would be a great help! or something like node.argString() that is mentioned in the comments for getDelimitedInputs. Either would be valuable if available in PowerShell!

@bryanmacfarlane where is the most appropriate place to discuss custom task development? this repo doesn't seem ideal, but it DOES have the most experienced audience.

Thread revival level = 100

Any news here? Discovering that my variable values (e.g. "text,one,two,three,four") is getting passed through as either:

  • A single space-delimited string, the commas removed, or;
  • null

Currently going through the motions of trying to escape/not-escape/single-quote-enclose etc. to figure out how to get this working.

Any update?

Was this page helpful?
0 / 5 - 0 ratings