Automation Engine Script Guide > Introduction > First Steps > Loops

Loops in Scripts

Objectives:
- WHILE loops
- PROCESS loops

Lesson 8

Loops process particular scripting lines several times. AE Script provides two different loops that can be used:

Loops repeat a particular block until a particular condition is met. The iteration ends when the condition does not apply any more.

Note that script processing will abort if a WHILE loop is iterated many times in succession. This behavior serves to avoid infinite loops.

A reference to a data sequence is passed on to the process loop. The number of loop cycles results from the data sequenceAn internal listing of Console outputs or lines of Variable objects, etc. The lines of a data sequence can be accessed by using a PROCESS loop or the script element GET_PROCESS_LINE. The script elements PREP_PROCESS* generate data sequences.'s number of lines. A data sequence is an internal listShows entities in a grid view (such as a list of files or the entries of Variable objects). The values that the data sequence includes depend on the particular script element.


The following example describes how to use WHILE loops.

The script checks whether a particular agentA program that enables the de-centralized execution of processes (such as deployments) on target systems (computers or business solutions) or a service that provides connectivity to a target system (such as for databases or middleware). An agent is also an object type in the Automation Engine. [Formerly called "Executor."] See also: host is available. If so, a jobAn Automation Engine object type for a process that runs on a target system. should start on this agent. The script can be located in any executable objectAutomation Engine controlled activities and processes are structured in the form of objects. See also: Task (such as SCRI). The only requirement is an agent on which the userIn the Automation Engine, a user is an instance of a User object, and generally the user is a specific person who works with Automic products. The User object is assigned a user ID and then a set of access rights to various parts of the Automation Engine system and product suite. These access rights come in the form of Automation Engine authorizations and privileges, Decision user roles and EventBase rights and ARA web application object rights. You can manage all these centrally in the ECC user management functions. See also, Unified user management. is allowed to execute jobs.

In the first step, use the script element SYS_HOST_ALIVE in order to verify that the agent is active. The following example uses the agent WIN03. The agent is available if the script element returns "Y".

:SET &AGENT# = WIN03
:SET &ALIVE# = SYS_HOST_ALIVE(&AGENT#)

The next step is to create a WHILE loop. It should repeatedly check the agent's availability with a particular delay (the intention is to avoid too many loop cycles).
The loop should end when the agent is active. Therefore, the loop condition must be: &ALIVE# NE "Y". The comparison operator "NE" corresponds to "Does not equal". Other comparison operators are described in the documentation of the script element :WHILE.

:WHILE &ALIVE# NE "Y"
: PRINT "Waiting for agent &AGENT#..."
: WAIT 60
: SET &ALIVE# = SYS_HOST_ALIVE(&AGENT#)
:ENDWHILE

:SET &ACT# = ACTIVATE_UC_OBJECT(TESTJOB
)

If the agent is not active, the loop instructions are repeated. The script statement :WAIT delays script processing for a while (in this case, the waiting time is 60 seconds).

If the agent is available again, the verification (SYS_HOST_ALIVE) returns the value "Y". The loop ends and the scripting line that activates the job "TESTJOB" (script functionPre-defined run book template in the Automation Engine. One single step only, e.g. Start Windows Service, Copy file,… ACTIVATE_UC_OBJECT) is read. The activation protocol indicates whether the system is still waiting for the agent or whether the job has already been activated.


The following steps describe the handling of process loops:

The objective is to obtain a file list from a computer and output it in the activation protocol.

The script element PREP_PROCESS_FILENAME is used to obtain the file list from an agent's computer. Filter keys serve to select files with particular names. The file list is provided as a data sequence in the script.

A process loop is required in order to process this data sequence line by line. It starts with :PROCESS and ends with :ENDPROCESS. For each line in the data sequence, the loop is processed once.

The script element GET_PROCESS_LINE retrieves the content of the current data sequence line. In this example, the result is the path and name of the file. It is output in the activation reportA report provides more detailed information about a task's execution or a component..

The following example uses the agent WIN01 in order to retrieve the file list. We retrieve all text files of the directory "C:\AUTOMIC\Agents\WIN01\temp" which includes the agent's log files.

:SET&HND# = PREP_PROCESS_FILENAME("WIN01", "C:\AUTOMIC\Agents\WIN01\temp\*.txt","Y",,)
:
PROCESS&HND#
:   
SET&LINE#=GET_PROCESS_LINE(&HND#)
:  
PRINT&LINE#
:
ENDPROCESS

The result is that the paths and names of all selected files are output in the activation protocol.

 


Automic Documentation - Tutorials - Automic Blog - Resources - Training & Services - Automic YouTube Channel - Download Center - Support

Copyright © 2016 Automic Software GmbH