Scripting with loops

Here, we will explore how to create loops in scripts. You will learn how to build loops and use them.

Getting ready

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.

How to do it...

There are several types of loop one can create; however, they can all be reduced to the following two basic types.

The decision loop

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.

The decision loop

  1. Create a new workflow and build the preceding schema.
  2. Add the following variables:

    Name

    Type

    Section

    Use

    number

    Number

    IN

    This is used to stop the loop

    counter

    Number

    Attribute

    This has the value 0 and count loop iterations

  3. Assign the counter attribute the initial value, 0.
  4. Bind the counter attribute to the IN and OUT sections of the Increase counter element.
  5. Bind the text in-parameter of System log to the counter. This will write the current count into the logs.
  6. In the Custom decision element, bind the counter and the in-parameter to the IN section.
  7. In the Scripting section, enter the following script:
          if (number == counter) { 
              return true; 
          } else { 
              return false; 
          } 
    
  8. Save and run the workflow.
  9. The workflow will run as many times as the value entered.

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.

The Foreach loop

A Foreach loop will repeat one workflow with different inputs. For the input, you must select an array.

The Foreach loop

  1. Create a new workflow and create the following variables:

    Name

    Type

    Section

    Use

    input

    Array of string

    IN

    This is an array of input variables

    output

    Array of string

    OUT

    This is an array of output variables

  2. Drag the Foreach element onto the schema. You will be asked what workflow you want to use with it. For simplicity, we will use the workflow 00.00 BasicWorkflow, which is stored in the folder, Basic Helper.
  3. Open the Foreach element in the IN section. You will see that the input is already bound. If in another workflow, you want to choose another iterator, click on Array(s) to be traversed, choose another array, and bind the variable.
  4. Bind the output variable.
  5. Save and run the workflow.

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.

Tip

Foreach workflows are run synchronously, meaning that if one fails the whole element will fail.

Have a look at the There's more... section to find out how to deal with exceptions in the Foreach element.

How it works...

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.

How it works...

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.

Types of decision loops

There are three types of decision loops:

Loop type

Description

for

A counting variable is used to count the number of runs. The loop terminates when the count has reached a predefined value.

do-while

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.

while-do

This is the same as the do-while loop, except the check is performed before any action is taken. If the check is true, the loop will not be run.

Foreach and arrays

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:

  1. Add the arrays to Array(s) to be traversed.
  2. Your arrays are now selectable when setting them:

    Foreach and arrays

JavaScript

JavaScript has the following loops:

Statement

Example

for

for (start,

condition,

increase) {

code block;

}

for (i = 0; i < 5; i++) {

System.log(i);

}

while

while (condition) {

code block;

}

do {

code block;

}

while (condition);

Var i = 0;

while (i < 10) {

System.log(i);

i++;

}

for each

for each (variable in array) {

code block;

}

for each (day in week) {

System.log(day);

}

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.

There's more...

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:

There's more...

See also

The example workflows:

  • 05.04.1 Decision-Loop
  • 05.04.2 ForEach-Loop
  • 05.04.3 DoWhile-Loop
  • 05.04.4 ForEach-Exceptions
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset