Here, we will explore how to create loops in scripts. You will learn how to build loops and use them.
We just need a working Orchestrator, and you will need the rights to create new workflows and run them. We will work with the Orchestrator Client.
You need to understand how decisions are used in Orchestrator; this was explained in the recipe Scripting with decisions.
For the Foreach element, we will be using the example workflow 00.00 BasicWorkflow
, which is stored in the Basic Helper
folder.
There are several types of loop one can create; however, they can all be reduced to the following two basic types.
This basic kind of loop runs until a certain condition is met. We will build a so-called for
loop in this example. A discussion about the different types of decision loops (for, do-while
, and while-do
) can be found in the How it works... section of this recipe.
Name |
Type |
Section |
Use |
|
Number |
IN |
This is used to stop the loop |
|
Number |
Attribute |
This has the value 0 and count loop iterations |
if (number == counter) { return true; } else { return false; }
What happens is that the decision will check whether the attribute counter is equal to the value entered; if it is not, the loop will run and increase the counter by one.
A Foreach
loop will repeat one workflow with different inputs. For the input, you must select an array.
Name |
Type |
Section |
Use |
|
Array of string |
IN |
This is an array of input variables |
|
Array of string |
OUT |
This is an array of output variables |
00.00 BasicWorkflow
, which is stored in the folder, Basic Helper
.When the workflow runs, you will be prompted to enter values into an array. The basic workflow will run for each element you have entered into the array. The result of each run will be stored in the array.
Have a look at the There's more... section to find out how to deal with exceptions in the Foreach element.
Loops are a very common tool in programming. They enable programs to go through repetitions. The two basic types we have introduced are different in the way they work. Decision loops use a condition to terminate, whereas the Foreach
loop terminates when all elements of the input have been processed.
There is a major difference between looping in JavaScript (such as inside a Scriptable task) and using the workflow schema. Control! When we loop in the schema we can control error handling, exceptions, attribute handling, and get a better grip on troubleshooting.
An example of a decision loop is a loop that checks for e-mails with a certain subject every minute. The loop in this example is actually a combination of a do-while
loop and a for
loop at the same time. The double loop is done to make sure the loop doesn't run forever. After 10 runs, the loop will terminate.
The classic example for a Foreach
loop is renaming multiple VMs. You define in one array the VMs you want to rename and in the other the new names.
There are three types of decision loops:
Loop type |
Description |
|
A counting variable is used to count the number of runs. The loop terminates when the count has reached a predefined value. |
|
An action is performed and, after that, the result of the action is checked against a condition. As in the previous e-mail example, we check whether the e-mail has arrived. This loop will run at least once. |
|
This is the same as the |
The Foreach element needs arrays for input and for output. However, if you create a normal (non-array) variable in General or Input, you can add it as an input parameter for the Foreach element, meaning that this would be a static value for all runs of the Foreach element. Please note that you still need at least one array as input.
An example for this discussion is the creation of 10 VMs that all have the same attributes except their name. You would use a Foreach loop on the Create VM workflow, the VM name would be an array, and all the others would be normal attributes.
If you want to add an attribute array to a Foreach element, then you need to follow these steps:
JavaScript has the following loops:
Statement |
Example | |
|
|
|
|
|
|
|
|
|
This is straightforward. condition
is like any other condition we explained in the recipe Scripting with decisions in this chapter. code
block
is any JavaScript code you would like to implement. The only thing that might need a bit of explanation is for each
. The (variable in array)
part defines a new variable
that is filled each time with a new element from the array
. For example, if we have an array that contains the days of the week, each time the loop is run the day
variable will be filled with another day.
When handling exceptions with the for each
loop, there are some extras you might find useful. Just adding the output exception will stop the for each
loop as soon as an error occurs. If you activate Catch any exception and continue with the next iteration, the for each
loop will not stop, but will continue. Additionally, you can add code that will be executed each time an exception happens in the loop. You have access to all the in-parameters of the for each
loop, but also the $index
variable, which contains the current iteration of the loop: