VMware Cloud Community
vMarkusK1985
Expert
Expert
Jump to solution

Get all vCD VMs from vCD Host

Hello,

I need to get all vCloud Director VMs related to a vCD Host in vRealize Orchestrator.

I already created this Script:

var adminhost = host.toAdminObject();

var AdminOrganizations = adminhost.getAdminOrganizations();

var VcloudVms = new Array();

for each (AdminOrganization in AdminOrganizations){

var AdminVdcs = AdminOrganization.getAdminVdcs();

for each (AdminVdc in AdminVdcs){

var VApps = AdminVdc.getVApps();

for each (VApp in VApps){

var Vms = VApp.getChildrenVms();

for each (Vm in Vms){

VcloudVms.push(Vm);

}

}

}

}

System.log(VcloudVms.length + " vCloud Vms Found");

But I think this can´t be the most efficient way to do that... Any recommendations?

Best regards,

Markus

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos
1 Solution

Accepted Solutions
legioon
Enthusiast
Enthusiast
Jump to solution

You can do this with query services of vRO ; It's too fast...

var isSystem;

if (host.organization.toLowerCase() == "system") {

isSystem = true;

} else {

isSystem = false;

}

var queryService = host.getQueryService()

var expression1 = new VclExpression(VclQueryVMField.NAME, "*", VclExpressionType.EQUALS);

var expression2 = new VclExpression(VclQueryVMField.ISVAPPTEMPLATE, false, VclExpressionType.EQUALS);

var expressions = new Array(expression1,expression2);

var filter = new VclFilter(expressions,VclFilterType.AND);

var params = new VclQueryParams();

var fields = new Array();

fields.push(VclQueryVMField.NAME);

params.addSortFields(fields, VclSortType.SORT_ASC);

params.setFilter(filter);

var resultSet;

if (isSystem == true) {

resultSet = queryService.queryRecords(VclQueryRecordType.ADMINVM, params);

} else {

resultSet = queryService.queryRecords(VclQueryRecordType.VM, params);

}

var vms = new Array();

while (resultSet != null)  {

    if (isSystem == true) var records = resultSet.getRecords(new VclQueryResultAdminVMRecord());

else var records = resultSet.getRecords(new VclQueryResultVMRecord());

for each (var record in records) {

var vmRef = new VclReference();

vmRef.href = record.href;

vmRef.name = record.name;

vmRef.type = record.type;

var vm = host.getEntityByReference(VclFinderType.VM, vmRef);

if (vm != null){

System.log(vm.name);

}

}

resultSet = resultSet.getNextPage();

}

View solution in original post

0 Kudos
2 Replies
legioon
Enthusiast
Enthusiast
Jump to solution

You can do this with query services of vRO ; It's too fast...

var isSystem;

if (host.organization.toLowerCase() == "system") {

isSystem = true;

} else {

isSystem = false;

}

var queryService = host.getQueryService()

var expression1 = new VclExpression(VclQueryVMField.NAME, "*", VclExpressionType.EQUALS);

var expression2 = new VclExpression(VclQueryVMField.ISVAPPTEMPLATE, false, VclExpressionType.EQUALS);

var expressions = new Array(expression1,expression2);

var filter = new VclFilter(expressions,VclFilterType.AND);

var params = new VclQueryParams();

var fields = new Array();

fields.push(VclQueryVMField.NAME);

params.addSortFields(fields, VclSortType.SORT_ASC);

params.setFilter(filter);

var resultSet;

if (isSystem == true) {

resultSet = queryService.queryRecords(VclQueryRecordType.ADMINVM, params);

} else {

resultSet = queryService.queryRecords(VclQueryRecordType.VM, params);

}

var vms = new Array();

while (resultSet != null)  {

    if (isSystem == true) var records = resultSet.getRecords(new VclQueryResultAdminVMRecord());

else var records = resultSet.getRecords(new VclQueryResultVMRecord());

for each (var record in records) {

var vmRef = new VclReference();

vmRef.href = record.href;

vmRef.name = record.name;

vmRef.type = record.type;

var vm = host.getEntityByReference(VclFinderType.VM, vmRef);

if (vm != null){

System.log(vm.name);

}

}

resultSet = resultSet.getNextPage();

}

0 Kudos
vMarkusK1985
Expert
Expert
Jump to solution

Thank you. Query Service is way faster!

I just added the push into the Array:

if (vm != null){ 

vms.push(vm);

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos