vRO 8.1 - Assigning values to Variables with Powershell and Scriptable tasks

One of the exiting new features in the vRA/vRO 8.1 platform is the ability to use Powershell as a native language in vRO. You can also use Python and node.js as well, in addition to the tradional Javascript.

One catch is that you need to have a vRA license to be able to use the new languages natively, meaning it doesn't work in a standalone vRO installation.

I haven't had as much time as I wanted to play around with vRO 8.x yet, but a recent task had me set out to explore it a bit further.

One thing that puzzled me a bit was how Powershell interacts with the built-in vRO variables (attributes in 7.x) and input/output constructs when using Scriptable tasks. To my surprise it's not really very well documented (at least not the output part).

Maybe it's obvious enough how output works, or I'm just lousy at googling (I actually found some examples in the Announcement post linked earlier just before publishing this post that gave away the answer, so probably my Google skills needs to be sharpened), but it took me a while to figure it out.

Anyways, here's a quick write up that might help others struggling

Task input

How a Powershell task can interact with inputs is documented here

Basically the Scriptable task puts inputs in a Powershell variable called $inputs which you can use in your Powershell code.

Let's look at an example:

Here's a Workflow with a variable called inputText with a value assigned.

Workflow variable

This is referenced in a scriptable task as an input with the same name.

Variable linked to task input

To use that in my Powershell code I'm accessing it through $inputs.inputText. Note that the variable property needs to match the name of the vRO input variable

Accessing input variable

Now, when I run that workflow I can see my variable value printed in the workflow log

Log output

Task output

Now, let's say I want to change the value, or use it to manipulate some other variable. In pure Powershell that can be done like this

Adding some text

Let's run the workflow to see our new variable printed

New log output

Ok, that works as you'd expect. But what if you want to use that new variable later on?

First we obviously need to add an output variable from the task. That's how vRO are handling stuff in and out of elements. So let's add a new variable to our workflow

Adding a new variable

Now, let's link this to the output of our Scriptable task

Linking variable to output

One might think that since the Powershell code by default comes with a return statement we could use that for assinging our output variable

Trying to return variable

But when we run the workflow, the variable remains empty

No value assigned

This kind of makes sense, since you might want to manipulate and assign values to multiple variables from a task and the function can only return once.

So how do you get stuff out?

Assign your output variables as properties to the PS output variable

Well, the answer is in the heading. We construct an output object, and assign the output through that. vRO is actually trying to show you this when you change the scripting runtime to Powershell.

Let's take a look at a new Scriptable task where we change the scripting runtime to Powershell

Default PS code

Notice that $output=@{status = 'done'} line? There's your answer.

So, going back to our initial Scriptable task. Let's see how we can assign a value to our vRO output variable.

We'll create an output object by using the name of the vRO output variable as the property key, and the PS variable as the property value

Creating an output object

Now let's run the workflow again, and check our variable

Output variable assigned

Great, we have our variable assigned with our output. Now we can use this in subsequent elements

Let's assign the outputText vRO variable as an input to the second Scriptable task and use that in our Powershell code

Assign variable manipulated by first task

Run the workflow to confirm

Workflow run with data from both tasks

Nice. Now we know how to pass stuff from one Scriptable task to another using the Powershell runtime in vRO.

Complex objects

One important note is that the type of the output property must match the type of the output variable in vRO. In this case I've used string. Had we used integers we would have updated the variable in vRO to reflect that. But what if our objects are more complex?

Well our old vRO friend, Properties, comes to the rescue

Let's create a new variable of type Properties

New variable of type Properties

And add it as an output from our first task

Add Properties object as output from task

Now, let's create an object in Powershell and return that from the task. Notice I'm creating a PSCustomObject as you can do in many different ways in Powershell and assigning that a PS variable name. Then I'm adding the complexObject vRO variable name to the $output object as a property key with my newly created object as the property value.

Creating a PS custom object and adding to output

Let's run the workflow and see what happens

Workflow run log

The log prints our object. Let's see if our vRO variable got assigned with our object properties

Object variable

Nice. Notice that vRO also figured out the correct type for each of the values.

Finally, let's see how we can utilize this new object as an input to a task by accessing the different properties.

Accessing complex object

Run the workflow one last time and check the log output

Workflow log

Summary

Using Powershell as the runtime language for scriptable tasks in vRO might make the product a bit more accessible for people not very conformant in JavaScript. I know my learning curve was quite steep when I first started using vRO, and I'm pretty sure it would have been easier if I could have used Powershell natively instead, so I'm quite exited about the Powershell capabilities in the product.

It is however quite the catch that it's only working with a vRA licensed version, and not in a standalone instance.

Anyways, hopefully this post has shown how to manipulate variables in vRO with Powershell.

Thanks for reading, and reach out if you have any questions or comments

This page was modified on June 22, 2021: Fixed typo