// array to hold the vm names var vmsWithSpecificTag = new Array(); // VAPI connection var endpoints = VAPIManager.getAllEndpoints(); //Use the first returned endpoint to gather information var client = endpoints[0].client(); //var client = end.client(); //Get associations var tagging = new com_vmware_cis_tagging_tag__association(client); //Get tags var tagMgr = new com_vmware_cis_tagging_tag(client); //Create object to hold VM var objId = new com_vmware_vapi_std_dynamic__ID() ; //Get all virtual machines vms = System.getModule("com.vmware.library.vc.vm").getAllVMs(); //loop though virtual machines for each (vm in vms) { // assign VM data to object objId.id = vm.id; // assign VM data to object objId.type = vm.vimType; //Get tags assigned to VM var tagList = tagging.list_attached_tags(objId); //Loop through VM assigned tags for each (var tagId in tagList) { //get object on tag var theTag = tagMgr.get(tagId); //assign name to compare var tagName=theTag.name; //compare to our requested tag if (tagName == tagNameToMatch) { System.log("VM : " + vm.name + " has the tag : " + tagName ); // add to array vmsWithSpecificTag.push(vm.name); } } } return vmsWithSpecificTag;