Highlighted
Contributor
Contributor

vRA / vRO 8.1 - Return an array from Powershell

I'm trying to use the new native powershell in vro actions but struggling to get the right syntax to return an array. Below is the example code that I’m using:

function Handler($context, $inputs) {

    $output=@()

    $output += "first string"

    $output += "second string"

write-host "first element type is $($output[0].gettype().name)"

write-host "second element type is $($output[1].gettype().name)"

write-host "output type name is $($output.gettype().name)"

write-host "output basetype name is $($output.gettype().basetype.name)"

    return $output

}

I have tried setting the return type to:

  1. string and checking the array box
  2. array and unchecking the array box
  3. array and checking the array box
  4. properties and checking the array box

I always get an error that says "Not an Array".

Would greatly appreciate help if anyone has managed to make it work.

Tags (4)
6 Replies
Highlighted
Enthusiast
Enthusiast

I choose the way of creating a PSCustomObject which gets returned as a properties object of that action. The return object has one property, which contains the array. This is working for me.

Have a look and my sample code here: vRO 8.1 scriptable task with PowerShell error: Function was stopped. Most likely because it hit an O...

0 Kudos
Highlighted
VMware Employee
VMware Employee

Looks like a you found a bug, it is reproducible for any array result from PowerShell. I will make sure to bring this up to the dev team. As a workaround I would suggest to wrap it in a complex object as V00Z11​ suggested

0 Kudos
Highlighted
Contributor
Contributor

Thanks for the responses, I'm glad it wasn't me doing something dumb.

I was intending to use the workflow to populate a dropdown in service broker as an external action. Can I do this with a complex type? Or will the dropdown only allow me to use workflows that return an array?

0 Kudos
Highlighted
VMware Employee
VMware Employee

Then the default JS comes to the rescue Smiley Happy

I would recommend to do the following workaround:

1. Make your original PowerShell Action (let's call it myAction, for the later use) return the result in an object

function Handler($context, $inputs) {

    $output=@()

    $output += "first string"

    $output += "second string"

    $returnObject = [PSCustomObject]@{ 

        Elements = $output

    } 

    return $returnObject

}

2. Create a wrapping JavaScript Action that will invoke the action from 1. and return it's array property

actionResult = System.getModule("com.mymodule").myAction();

return actionResult.Elements;

3. In service broker use the wrapping action from 2.

0 Kudos
Highlighted
VMware Employee
VMware Employee

Another workaround  you can try out  is to return the output array  as single object

Something like

function Test-Return { $array = 1,2,3 return (, $array) }



So in your case you can try replacing

   return $output

with

  return (, $output)

Highlighted
VMware Employee
VMware Employee

yes, much simpler and does not require a second wrapping action

0 Kudos