Scriptable tasks and the schema canvas
The schema canvas is where you’ll spend most of your time in Orchestrator. Recipe 3.1 dropped two scriptable tasks onto it. This recipe goes further: you write a small reusable action, call it from a workflow with an action element, promote its parameters with one click, and process its result in a scriptable task. Along the way you’ll learn the canvas moves you need every day.
The workflow you build, List VMs by name, finds every VM whose name matches a pattern and returns their names and power states.
Getting ready
- The login, vCenter, and folder from recipe 3.1.
- An action module called
com.vcfguide.chapter3. You create it in step 1 if it doesn’t exist yet.
How to do it
Write the action
-
Go to Library > Actions and click New Action. Enter:
- Name:
findVmsByName - Module:
com.vcfguide.chapter3(type it to create the module)
Click Create.
- Name:
-
On the Script tab, set the Return type to
Array/VC:VirtualMachineand add one input,namePattern, of typestring. -
Enter this script, then click Save:
// Returns every VM, in every registered vCenter, whose name matches namePattern.// namePattern is a regular expression, matched without regard to case.var pattern = new RegExp(namePattern, "i");var allVms = VcPlugin.getAllVirtualMachines();var matches = [];for (var i = 0; i < allVms.length; i++) { if (pattern.test(allVms[i].name)) { matches.push(allVms[i]); }}System.log(matches.length + " of " + allVms.length + " VMs match /" + namePattern + "/");return matches;Start the workflow and call the action
-
Create a workflow called
List VMs by namein VCF Orchestrator Guide/Chapter 3, as in recipe 3.1, steps 1–3. -
Open the Schema tab. In the palette on the left, type
findVmsin the search box. Drag findVmsByName onto the arrow between the start and end elements. Orchestrator wraps it in an action element. -
A banner at the top of the canvas says the element’s inputs and outputs aren’t bound yet. Click Promote. In the dialog:
- For the input
namePattern, choose Input. Orchestrator creates a workflow input of the same name and type. - For the output
actionResult, choose Variable. Orchestrator creates a variable of typeArray/VC:VirtualMachine.
Click Promote.
- For the input
-
On the Variables tab, rename the new variable from
actionResulttovms. Its binding follows the new name.
Schema tab of List VMs by name, Promote dialog open for the findVmsByName element, namePattern set to Input and actionResult set to Variable.
Process the result in a scriptable task
- On the Inputs/Outputs tab, add an output
vmNamesof typeArray/string. - Back on the Schema tab, drag a Scriptable task from the Generic group onto the arrow after the action element. Name it
Describe each VM, bindvmsas its input andvmNamesas its output, and enter this script:
vmNames = [];for (var i = 0; i < vms.length; i++) { var line = vms[i].name + " (" + vms[i].runtime.powerState.value + ")"; vmNames.push(line); System.log(line);}if (vmNames.length === 0) { System.warn("No VM name matches the pattern");}Tidy the canvas
- Drag a Workflow note from the Generic group behind the two elements and type
Find and describe VMs by name pattern. Notes don’t run. They label a part of the schema for the next reader. - Use the zoom and fit controls at the bottom of the canvas to see the whole schema, then click Save.
- Run the workflow with
namePatternset to a pattern that matches some VMs in your lab. Our lab has three VMs calledtestvm01totestvm03, so figure 3.6 uses^testvm. The Logs tab shows the count from the action, then one line per VM. The Variables tab showsvmsandvmNames.


actionResult, that renaming a variable on the Variables tab keeps the element’s binding, and the names of the zoom and fit controls on the canvas.How it works
The canvas is a picture of the workflow’s logical flow: which element runs after which. Every element has one normal way out, drawn as a solid arrow, and most can also have an error path (recipe 3.4). When you drop an element onto an existing arrow, Orchestrator splits the arrow and inserts the element. To join two elements by hand, drag from the connector on the edge of one element to the other. To remove a link, select it and delete it.
The palette offers three kinds of thing to drag:
- Elements, in the Generic group: the building blocks such as scriptable tasks, decisions, switches, loops, waits, and the start and end elements.
- Predefined tasks, in the Basic, Log, and Network groups: small ready-made scripts you can’t edit, such as writing a log line or sending a notification.
- Workflows and actions from the library. A workflow becomes a workflow element, which runs it as a sub-workflow and waits for it to finish. An action becomes an action element.
An action element and a scriptable task both run a script, but they’re stored differently. A scriptable task’s script lives inside the workflow. Nobody else can call it, and copying it means copying text. An action lives once in the library, under its module, and any workflow, action, or form can call it. In a script you call it like this:
var vms = System.getModule("com.vcfguide.chapter3").findVmsByName("^vc");Promote saves you from declaring parameters by hand. For each parameter of the element, it creates a workflow input, output, or variable with the same name and type, and binds it. You can also skip a parameter and bind it yourself later.
System.log, System.warn, and System.error write to the run’s log at different levels. The log is kept with the run, so it’s the first place to look when a run goes wrong. For a line that must survive a server restart, use Server.log instead, which writes a persistent entry to Orchestrator’s database. It’s still kept with the run, so it goes when the run is purged. On 9.1.1, those entries show on the run’s Logs tab too, in purple, and the SERVER button above the log shows or hides them (recipe 4.6).
There's more
Scriptable task or action? A rule of thumb: if the script is more than about 20 lines, if you’d write it twice, or if you want to test it on its own, make it an action. Keep scriptable tasks for glue: reshaping one element’s output into the next one’s input, as in step 9.
Other languages. The Scripting tab of a scriptable task lets you choose a different runtime, such as Python, Node.js, or PowerShell, instead of JavaScript. Non-JavaScript scripts run in a separate container with their own handler function and are noticeably slower to start. Chapter 4 covers them, and the environments that hold their libraries.
Big inventories. VcPlugin.getAllVirtualMachines() returns every VM in every registered vCenter, which is slow when there are thousands. The function also accepts an XPath filter, so the vSphere plug-in does the matching for you:
var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[starts-with(., 'vc')]");The filter is XPath 1.0 over each object’s properties. Tested on 9.1.1 against the lab’s VMs: name='testvm01', name[starts-with(.,'testvm')], name[contains(.,'linux')] and name[translate(.,'TESTVM','testvm')='testvm01'] (a case-insensitive match) all work. XPath 2.0 functions such as matches don’t: they return no VMs and no error, so a typo in a filter looks like an empty inventory.
Keyboard and mouse. Select several elements by dragging a box around them on an empty part of the canvas. Copy and paste works within a workflow and between workflows, which is quicker than re-creating a scriptable task with its bindings.
Validate before you save. The Validate button at the top of the editor checks for unbound parameters, unreachable elements and missing end elements. Saving validates too: when something is wrong, 9.1.1 shows Validation is not successful. Are you sure you want to continue? with View details and Continue anyway. Saving anyway is fine while you build, but fix what it reports before you run the workflow. Recipe 3.10 covers Validate, Find usages and Find dependencies.
Business status . Every element has a Business status box on its Details section (on by default). Type a short label, such as Waiting for vCenter, and a run shows it while it’s on that element, which tells someone watching a long run where it is in plain words.
See also
- 3.1 Workflow basics, for inputs, outputs, variables, and bindings.
- 3.3 Decisions, switches, and loops, for elements that change the path through the schema.
- Broadcom TechDocs (VCF 9): Schema Elements and Schema Element Properties and Building a Workflow in the Workflow Schema.
- VMware Docs (vRealize Orchestrator 8.x): Schema elements and Promote input and output parameters.
