Creating JSON objects in vRO is pretty simple and straight forward. I mainly use two different methods, depending on the complexity. Method one is creating the object using string concatenation (useful for small and simple objects), and method two is the Properties object, which is, my opinion, better for more complex objects, especially if initially I don’t know how many entries the object is going to have or adding arrays.
As we can see, method one is straight forward. I added an array of strings in my example, but it could be an array of properties again. Instead of adding a string as value, input parameters of the scriptable task could be used too, just be careful with the type.
The second method, as we all can see, is easier and provides a better readability, especially if it comes to large objects.
For XML we can use two different main methods, but I am sure, there are other possibilities as well. I am using these two, because, especially the second one, it’s easy and straight forward.
In the first method, I use simple string concatenation. If the XML object is small, this is a fast and simple approach. It even allows adding dynamic values. As an example, I am going to reuse NSX stuff, creating the XML for the NSX controller deployment, because this is a small and understandable XML structure.
This example uses simple string concatenation. Please recognize the square brackets, surrounding all the strings, with the .join method at the end. For “name” and “password” I added dynamic values which are in parameters for the scriptable task to demonstrate how to add dynamic values. The downside of this method is, the result is a string and not an XML object which makes it less flexible for later editing. Of course, it could be converted later.
I am using the same XML but this time I create an E4X object straight ahead, which allows to easily add and/or change values. This is very useful for large XML objects. Be aware, if you’re not changing the value of a specific tag, the value provided during the object creation will remain.
Now that we’ve created the XML object, we need to add/change the values:
Using E4X allows us accessing the tags using a dot. This is great if we deal with larger XML data structures and especially nested structures. Be aware, the second method creates an XML object. If you would like to use it for REST calls, it needs to be converted into string upfront using the .toString() method. I our example, you would do the following:
Creating larger powerShell scripts might be tricky in vRO. I am currently using the already mentioned string concatenation method. It works great for all kind of scripts created for diverse command lines like bash.
For this article, I am using a, what I think, very useful example. I had to deal with powerShell scripts being identical but are executed against various hosts in different domains. I guess, we’re all aware of the double hop issue we have and all the different necessary configurations to get this up and running. What I do is creating a PSCred object on the fly and add it to the script as authentication method used with different cmdlet’s.
var powerShellScript =
In this example, I haven’t created the $computer variable. As you can see, this is an easy approach adding a dynamic credential object into a powerShell script to authenticate against various destinations without dealing with the double hop issue.
PLEASE NOTE: the credentials example used for powerShell is just an idea and has not been validated if it is a secure method and works only, if the used cmdlet supports the -credential option.
Have fun creating and using the different data objects.