Hi,
i am using the workflow: Create snapshots of all virtual machines in a ressource pool.
now i want to check in that workflow if a virtual machine has the tag xyz. if so, then do a snapshot. if not, do no snapshot. i think the best way is to get all vms with this tag in initialize part of that workflow in variable "allVMs", but how can i achive this? Or does anyone have an better idea?
regards,
bkgc
i have tried this but does not work:
var endpoint = VAPIManager.findEndpoint("https://xxx.xxxxxxxxxx.xxx/api");
var client = endpoint.client();
try {
var tagging = new com_vmware_cis_tagging_tag(client);
var result = tagging.list();
var tagassoc = new com_vmware_cis_tagging_tag__association(client);
var VMName = new VcVirtualMachine();for (i = 0; i < result.length; i++) {
var tag = tagging.get(result[i]);
if (tag.name == "DoSnapshot") {
var objs = tagassoc.list_attached_objects(result[i]);
for (j = 0; j < objs.length; j++) {
if(objs[j].type == "VirtualMachine") {
System.log(objs[j].id);if (VMName.id == objs[j].id ) {
allVMs = VMName;
}
}
}
}
}} finally {
client.close();
}
i also defiend an output parameter "allVMs" as Array of VC:VirtualMachine. But allVMs has no values in it when i run that script.
regards,
any ideas?
tried now this way, again with no luck:
var endpoint = VAPIManager.findEndpoint("https://derzvc01.onescope.net/api");
var client = endpoint.client();
try {
var tagging = new com_vmware_cis_tagging_tag(client);
var result = tagging.list();
var tagassoc = new com_vmware_cis_tagging_tag__association(client);
//var VM = new VcVirtualMachine();
var vms = VcPlugin.getAllVirtualMachines();
//System.log(vms);
for (i = 0; i < result.length; i++) {
var tag = tagging.get(result[i]);
if (tag.name == "DoSnapshot") {
var objs = tagassoc.list_attached_objects(result[i]);
for (j = 0; j < objs.length; j++) {
if(objs[j].type == "VirtualMachine") {
//System.log(objs[j].id);
for (k = 0; k < vms.length; k++) {
//System.log(vms[k].id);
if (vms[k].id == objs[j].id ) {
//System.log(vms[k]);
allVMs = vms[k];
}
}
}
}
}
}} finally {
client.close();
}
regards,
What's not working with this code? Did you see any output from commented debug logs on lines 18, 21 and 23?
One problem here is with the code on line 24. I assume you want to collect all vms[k] objects into an array allVMs, right? The code as written won't do it.
So assuming at some point (before the 'for' loops) you have declared allVMs variable to be an array with something like
var allVMs = [];
Then you should replace the code on line 24 with something like that will add vms[k] to the array:
allVMs.push(vms[k]);
Hi,
back from a short Holiday
i changed the code to yout suggestions now and it works.
Then i have removed the "Initialize" part from workflow "Create snapshots of all virtual machines in a ressource pool" and put my new created workflow "Find all VMs with tag "DoSnapshot"" in instead. My new workflow has output parameters allVMs, vmCount and currentVM. Same like in "Initialize". And i have renamed the Workflow to "Create snapshots of all virtual machines with tag "DoSnapshot"".
This is the final code from my new created workflow:
var endpoint = VAPIManager.findEndpoint("https://xxx.xxxxxxxxxx.xxx/api");
var client = endpoint.client();
try {
var tagging = new com_vmware_cis_tagging_tag(client);
var result = tagging.list();
var tagassoc = new com_vmware_cis_tagging_tag__association(client);
//var VM = new VcVirtualMachine();
var vms = VcPlugin.getAllVirtualMachines();
var allVMs = [];
//System.log(vms);
for (i = 0; i < result.length; i++) {
var tag = tagging.get(result[i]);
if (tag.name == "DoSnapshot") {
var objs = tagassoc.list_attached_objects(result[i]);
for (j = 0; j < objs.length; j++) {
if(objs[j].type == "VirtualMachine") {
//System.log(objs[j].id);
for (k = 0; k < vms.length; k++) {
//System.log(vms[k].id);
if (vms[k].id == objs[j].id ) {
//System.log(vms[k]);
allVMs.push(vms[k]);
}
}
}
}
}
}
vmCount = allVMs.length;
currentVM = 0;} finally {
client.close();
}
Works!!!
All VMs with tag "DoSnaphot" are snapshoted automaticly.
Thank you very very much for your help!!!!
regards,