Turning strings into objects
In this recipe, we will take a quick look at how to turn a string into an Orchestrator object (such as VC:VirtualMachine
). This technique is rather important when you use REST to start workflows.
Getting ready
We only need the Orchestrator Client with the right to create a workflow.
How to do it...
In this example, we turn a string into VC:VirtualMachine
:
Create a workflow with a string input (
vmString
) and aVC:VirtualMachine
output (vmObject
).Add a scriptable task and connect the in- and output parameter.
In the script, enter the following code:
query = "xpath:name='" + vmString + "'"; vms=Server.findAllForType("VC:VirtualMachine", query); vmObject=vms[0];
Run the workflow and enter a Virtual Machine name.
Check the output and logs.
How it works...
The find
function looks for all elements of a given type and can be limited using a search
function. It's very important to write the correct type and the search string.
The next important...