Decisions, switches, and loops
A straight line of steps only gets you so far. Real processes ask questions: is the VM on, did the guest come up, are there more items to handle? Orchestrator answers them on the canvas with three elements: the decision, the switch, and the foreach. A loop is simply an arrow that points back to an earlier element.
This recipe builds Wait for VMware Tools, which checks a VM’s power state, then polls until VMware Tools is running or it runs out of attempts. A second, short workflow then runs recipe 3.1’s Describe a VM once for each VM in a list.
Getting ready
- The
Describe a VMworkflow from recipe 3.1, and the folder VCF Orchestrator Guide/Chapter 3. - A VM in the lab with VMware Tools installed, so you can watch the loop end successfully. A powered-off VM is useful for testing the other path.
How to do it
Set up the workflow
- Create a workflow called
Wait for VMware Toolsin VCF Orchestrator Guide/Chapter 3. - On the Inputs/Outputs tab, add an input
vmof typeVC:VirtualMachineand an outputresultof typestring. - On the Variables tab, add these variables:
| Name | Type | Value |
|---|---|---|
powerState |
string |
(empty) |
toolsRunning |
boolean |
false |
attempt |
number |
0 |
maxAttempts |
number |
10 |
waitSeconds |
number |
30 |
Branch on the power state with a switch
- On the Schema tab, add a Scriptable task called
Read power state, withvmin andpowerStateout:
powerState = vm.runtime.powerState.value;System.log(vm.name + " is " + powerState);- Drag a Switch from the Generic group onto the arrow after
Read power state. - Add a Scriptable task called
Not powered onbelow the switch, withvmandpowerStatein andresultout, and link it to an end element:
result = vm.name + " is " + powerState + ", so there is nothing to wait for";System.log(result);- Add a Scriptable task called
Check VMware Toolsto the right of the switch. Bindvm,attempt,maxAttempts, andwaitSecondsas inputs, andattempt,toolsRunning, andresultas outputs:
attempt = attempt + 1;var toolsStatus = vm.guest.toolsRunningStatus;toolsRunning = (toolsStatus === "guestToolsRunning");result = vm.name + ": VMware Tools is " + toolsStatus + " after " + attempt + " check(s)";System.log("Check " + attempt + " of " + maxAttempts + ": " + toolsStatus);
// Pause before the next check, but not after the last oneif (!toolsRunning && attempt < maxAttempts) { System.sleep(waitSeconds * 1000);}- Select the switch and name it
Powered on?. On its Cases tab, click Add new switch case: IfpowerStateequalspoweredOn, then go toCheck VMware Tools. Under By default go to, pickNot powered on. The arrows on the canvas follow what you pick here.

Decide whether you’re done
- Drag a Decision onto the arrow after
Check VMware Tools. Name itTools running?. Leave Decision type on Simple, and in the Decision section set If totoolsRunningis true. A decision has two arrows: the solid green one is the true path, the dashed red one the false path. - Link the decision’s true path to an end element.
Loop back if not
- Drag another Decision onto the canvas and link the first decision’s false path to it. Name it
Try again?and turn off the Decision type switch (Simple), so you can write a script. The section text says it plainly: Create custom condition and write yourself javascript function or turn on simple mode. BindattemptandmaxAttemptsas inputs, and enter this script, which must returntrueorfalse:
return attempt < maxAttempts;- Link the scripted decision’s true path back to
Check VMware Tools. That arrow is the loop. - Link its false path to an end element. The workflow ends there when it runs out of attempts, with
resultsaying what VMware Tools last reported. Recipe 3.4 shows how to make this case fail the run instead. - Click Save.

- Run it against a powered-on VM. The log shows one line per check until VMware Tools reports
guestToolsRunning. Run it against a powered-off VM. The run goes straight down the default path.
On 9.1.1 the two runs took a fraction of a second each: against a powered-on VM the result was rockylinux9: VMware Tools is guestToolsRunning after 1 check(s), and against a powered-off VM testvm01 is poweredOff, so there is nothing to wait for.
Run a workflow for each VM in a list
- Create a workflow called
Describe several VMsin the same folder. - On the Schema tab, drag a Foreach element from the Generic group onto the arrow between start and end, and choose the workflow
Describe a VM. - Click Promote in the banner. Choose Input for
vmand Output forsummary. Because they’re promoted from a foreach element, the new input is an array,Array/VC:VirtualMachine, and the new output isArray/string. - Save and run it. On the run form, add three VMs to
vm. The run callsDescribe a VMthree times, one VM each time, and the output holds the three summaries in the same order.

How it works
The three branching elements work the same way underneath: they read slots and pick which arrow to follow next. They don’t change data, so there’s nothing to bind as an output. The foreach element is different: it runs a whole sub-workflow and can collect its outputs.
- A decision in Simple mode compares one input, a parameter or variable, with a value, and follows its true or false path. You fill in a form, not a script, so it’s the easiest to read.
- A decision with Simple turned off runs a script with as many bound inputs as you like and follows the path that matches its return value. Use it when the condition needs more than one slot or any arithmetic.
- A switch checks its cases in order and follows the first one that matches. If none does, it follows the default path. It’s a tidier way to write three or more decisions in a row.
- A foreach element runs a sub-workflow once for each item of the arrays you give it. A parameter that isn’t an array gets the same value on every run. When the sub-workflow has outputs, the foreach element collects one value per run into an array.
A loop isn’t a separate element. Orchestrator simply follows the arrow back to Check VMware Tools, which runs again with the current values of attempt and the other slots. Because every step’s slots are saved after it runs, a loop that’s interrupted by a restart picks up at the check it was on.
vm.guest.toolsRunningStatus comes from vCenter, through the vSphere plug-in. Each time the scriptable task reads it, the plug-in returns the property’s current value.
vm.guest.toolsRunningStatus between loop iterations. If it does, the loop needs a refresh call before each read.There's more
Don’t sleep for long. System.sleep keeps the run busy and holds a thread on the server. That’s fine for seconds or a few minutes. For anything longer, such as waiting an hour for a change window, use a waiting timer element instead, which pauses the run without using the server’s resources (recipe 3.7).
Always bound a loop. Every loop needs a way out that doesn’t depend on the outside world. maxAttempts makes sure this one ends even if VMware Tools never starts. Without it, a VM with broken Tools would keep the run going for ever.
Composite types. When a foreach element needs to iterate over pairs of values, for example a VM and its new name, promote them as one composite type. Each item then holds both values, so they can’t get out of step. Workflows with composite-type inputs can’t be run from every client, so keep them for workflows that other workflows call.
Switches on numbers. A switch case can compare more than strings. Use it on a number, such as the number of CPUs, with conditions such as greater than or less than.
Failures inside a foreach. If one run of the sub-workflow fails, the foreach element fails too, and the items after it aren’t processed. Tested on 9.1.1 with [testvm01, null, testvm01]: the second run failed with Cannot read property “runtime” from null, the third never ran, and the output held only the first summary. To carry on past a failed item, turn on Iteration error handler on the foreach element, or catch the error inside the sub-workflow as in recipe 3.4.
See also
- 3.1 Workflow basics, for the
Describe a VMworkflow used by the foreach element. - 3.4 Handling errors and exceptions, to make the give-up path fail the run.
- Broadcom TechDocs (VCF 9): Schema Elements and Schema Element Properties and Logical Flow of a Workflow.
- Broadcom TechDocs (Aria Automation Orchestrator): Foreach Elements and Composite Types and Add a Switch Activity to a Workflow.
- VMware Docs (vRealize Orchestrator 8.x): Using a foreach element.
- CloudBlogger, the author’s blog: Cyclic Workflows in Aria Automation Orchestrator.
