WJCon
Contributor
Contributor

Loop through VMs in a vCD vAPP and return vm name

Jump to solution
I am trying to loop through a series of VMs in a vapp and run and if statement based on the name of the vm - but at the moment I dont seem to be able to pull back the vm name - all that comes back is 'undefined' - why is vmname being undefined if i am setting it to the vm.name - am i missing a step? I feel like i am missing something simple but cant find any similar online
 
 
var allVMs = System.getModule("com.vmware.library.vCloud.vApp").getVmsFromVApp(vappout); 
for (vm in allVMs){
System.log(vm.name); 
var vmname = vm.name
if (vmname = "test"+*)  { //if the vmname starts with test do the below
System.log("Match vm with name " + vmname
 
}
{
System.log ("No match with name " + vmname)
}
Labels (1)
0 Kudos
1 Solution

Accepted Solutions
Jeff_CH
Enthusiast
Enthusiast

A possibility would be to use the substring function, like this:

 

If (vmname.substring(0,5) == 'test-') {

 

with 0 begin the starting index and 5 the end index.

View solution in original post

3 Replies
WJCon
Contributor
Contributor

ok so changed it to 

for (i in allVMs){
var vmname = allVMs[i].name
System.log (vmname)
 
This is returning back as expected so just need to solve the comparison part now
0 Kudos
WJCon
Contributor
Contributor

The only thing I am missing now is the string comparison basically I am not sure what the syntax is when matching a wildcard.

So I am expecting something similar to

 

for (i in allVMs){
var vmname = allVMs[i].name
System.log (vmname)

If (vmname = 'test-' + *) {

System.log('Match found!' + vmname)

//Do some stuff

}{

System.log('Does no match ' + vmname)

//Do Nothing

}

 

I seem to be slipping up on the wildcard part - does anyone know what the syntax would be for that line to match a pattern with a wildcard at the end. I expected it would include * but I think it is now just seeing * as a character.

 

 

0 Kudos
Jeff_CH
Enthusiast
Enthusiast

A possibility would be to use the substring function, like this:

 

If (vmname.substring(0,5) == 'test-') {

 

with 0 begin the starting index and 5 the end index.

View solution in original post