VMware Cloud Community
RichardSillito
Enthusiast
Enthusiast
Jump to solution

Recursively Act on vm's in subfolders

Hi All,

Hoping someone has already done this. I want to create a workflow that will shutdown all vm's in Tenant1 folder. The problem is that if you use the getAllVirtualMachinesbyFolder it does not perform a recursive search. So in this example it will find no virtual machines.

Capture.PNG

Does someone have a workflow example that will do the action recursively?

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

There is an action getAllVirtualMachinesByFolderIncludingSubFolders that takes a VmFolder input parameter and returns an array with virtual machines in this folder and its subfolders (performing a recursive search).

Here is the action's scripting code for reference:

if (!vmFolder) {

  throw "UndefinedParameter: vmFolder mandatory input is not defined.";

}

var allVms = new Array();

getAllVmsInFolder(vmFolder);

return allVms;

function getAllVmsInFolder(folder) {

  var children = folder.childEntity;

  for (var i in children) {

  if (children[i] instanceof VcFolder) {

  getAllVmsInFolder(children[i]);

  }

  if (children[i] instanceof VcVirtualMachine) {

  allVms.push(children[i]);

  }

  }

}

View solution in original post

0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

There is an action getAllVirtualMachinesByFolderIncludingSubFolders that takes a VmFolder input parameter and returns an array with virtual machines in this folder and its subfolders (performing a recursive search).

Here is the action's scripting code for reference:

if (!vmFolder) {

  throw "UndefinedParameter: vmFolder mandatory input is not defined.";

}

var allVms = new Array();

getAllVmsInFolder(vmFolder);

return allVms;

function getAllVmsInFolder(folder) {

  var children = folder.childEntity;

  for (var i in children) {

  if (children[i] instanceof VcFolder) {

  getAllVmsInFolder(children[i]);

  }

  if (children[i] instanceof VcVirtualMachine) {

  allVms.push(children[i]);

  }

  }

}

0 Kudos
RichardSillito
Enthusiast
Enthusiast
Jump to solution

Thank you!

That worked perfect!

RS

0 Kudos