VMware Cloud Community
Subnet88
Enthusiast
Enthusiast
Jump to solution

List of VMs in Vapp using the Query service

Has anyone been able to use the query service to output an array of VMs in a Vapp? I do not see any attributes on the VclQueryadminVMField that reference the vApp.

I am logging into the System Org.

Any help would be appreciated.

1 Solution

Accepted Solutions
Burke-
VMware Employee
VMware Employee
Jump to solution

Action name: getVAppVms

Input: vApp (vCloud:VApp)

Output: Array/vCloud:VM

Script:

var vcdHost = vApp.getHost();
var vms = new Array();
var queryService = vcdHost.getQueryService();
expression = new VclExpression(VclQueryVMField.CONTAINER, vApp.getReference().href, VclExpressionType.EQUALS);
filter = new VclFilter(expression);
params = new VclQueryParams();
params.setFilter(filter);
var resultSet = queryService.queryRecords(VclQueryRecordType.VM, params);
while (resultSet != null) {
    var records = resultSet.getRecords(new VclQueryResultVMRecord());
    System.log(records.length + " VM records found");   
    for each (var record in records) {
     var vmRef = new VclReference();
     vmRef.href = record.href;
     vmRef.name = record.name;
     vmRef.type = record.type;
     vms.push(vcdHost.getEntityByReference(VclEntityType.VM, vmRef));
    }
    resultSet = resultSet.getNextPage();
}
return vms;

Here's what our team has done (specifically - Christophe Smiley Happy )

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter

View solution in original post

2 Replies
Burke-
VMware Employee
VMware Employee
Jump to solution

Action name: getVAppVms

Input: vApp (vCloud:VApp)

Output: Array/vCloud:VM

Script:

var vcdHost = vApp.getHost();
var vms = new Array();
var queryService = vcdHost.getQueryService();
expression = new VclExpression(VclQueryVMField.CONTAINER, vApp.getReference().href, VclExpressionType.EQUALS);
filter = new VclFilter(expression);
params = new VclQueryParams();
params.setFilter(filter);
var resultSet = queryService.queryRecords(VclQueryRecordType.VM, params);
while (resultSet != null) {
    var records = resultSet.getRecords(new VclQueryResultVMRecord());
    System.log(records.length + " VM records found");   
    for each (var record in records) {
     var vmRef = new VclReference();
     vmRef.href = record.href;
     vmRef.name = record.name;
     vmRef.type = record.type;
     vms.push(vcdHost.getEntityByReference(VclEntityType.VM, vmRef));
    }
    resultSet = resultSet.getNextPage();
}
return vms;

Here's what our team has done (specifically - Christophe Smiley Happy )

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
Subnet88
Enthusiast
Enthusiast
Jump to solution

Thank you!!

0 Kudos