Workflow scripted elements, actions, and policies require basic scripting of common tasks. You can cut, paste, and adapt these examples into your scripted elements.

Access XML Documents

The following JavaScript example allows you to access XML documents from JavaScript by using the ECMAScript for XML (E4X) implementation in the Orchestrator JavaScript API.

Note:

In addition to implementing E4X in the JavaScript API, Orchestrator also provides a Document Object Model (DOM) XML implementation in the XML plug-in. For information about the XML plug-in and its sample workflows, see the Using vCenter Orchestrator Plug-Ins.

var people = <people>
                 <person id="1">
                     <name>Moe</name>
                 </person>
                 <person id="2">
                     <name>Larry</name>
                 </person>
             </people>;

System.log("'people' = " + people);

// built-in XML type
System.log("'people' is of type : " + typeof(people)); 

// list-like interface System.log("which contains a list of " +
people.person.length() + " persons"); 
System.log("whose first element is : " + people.person[0]);

// attribute 'id' is mapped to field '@id'
people.person[0].@id='47'; 
// change Moe's id to 47 
// also supports search by constraints
System.log("Moe's id is now : " + people.person.(name=='Moe').@id);

// suppress Moe from the list
delete people.person[0];
System.log("Moe is now removed.");

// new (sub-)document can be built from a string 
people.person[1] = new XML("<person id=\"3\"><name>James</name></person>");
System.log("Added James to the list, which is now :");
for each(var person in people..person)

for each(var person in people..person){
	System.log("- " + person.name + " (id=" + person.@id + ")"); 
}

Setting and Obtaining Properties from a Hashtable

The following JavaScript example sets properties in a hashtable and obtains the properties from the hashtable. In the following example, the key is always a String and the value is an object, a number, a Boolean, or a String.

var table = new Properties() ; 
table.put("myKey",new Date()) ;
// get the object back 
var myDate= table.get("myKey") ;
System.log("Date is : "+myDate) ;

Replace the Contents of a String

The following JavaScript example replaces the content of a String and replaces it with new content.

var str1 = "'hello'" ; 
var reg = new RegExp("(')", "g"); 
var str2 = str1.replace(reg,"\\'") ; 
System.log(""+str2) ; // result : \'hello\'

Compare Types

The following JavaScript example checks whether an object matches a given object type.

var path = 'myurl/test';
if(typeof(path, string)){
  throw("string");
else {
  throw("other");
}

Run a Command in the Orchestrator Server

The following JavaScript example allows you to run a command line on the Orchestrator server. Use the same credentials as those used to start the server.

Note:

Access to the file system is limited by default. To access the file server from Orchestrator, see Accessing the Orchestrator Server File System from JavaScript and Workflows.

var cmd = new Command("ls -al") ;
cmd.execute(true) ;
System.log(cmd.output) ;