jvm2016
Hot Shot
Hot Shot

foreach_vRO_finding vm name

Jump to solution

Hi all,

could someone check the following simple workflow where in we want to find the names of vms in vcenter..

schema dia:

pastedImage_0.png

javascript code in scriptable task.

pastedImage_1.png

please suggest what neds to modified to get name of vms.

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee

Hi,

Here is a working code snippet:

var allVMs = VcPlugin.getAllVirtualMachines();

for each (vm in allVMs) {

  System.log("vmname: " + vm.name);

}

Differences compared to your code:

  • no need to call the action to get all VMs; there is a direct API to do that
  • for each is a two-words keyword, not foreach
  • vm.name is shorter variant of vm.summary.config.name

View solution in original post

0 Kudos
5 Replies
iiliev
VMware Employee
VMware Employee

Hi,

Here is a working code snippet:

var allVMs = VcPlugin.getAllVirtualMachines();

for each (vm in allVMs) {

  System.log("vmname: " + vm.name);

}

Differences compared to your code:

  • no need to call the action to get all VMs; there is a direct API to do that
  • for each is a two-words keyword, not foreach
  • vm.name is shorter variant of vm.summary.config.name
0 Kudos
jvm2016
Hot Shot
Hot Shot

Thanks for your reply.

Iam checking this code. however from my past experince with powercli   "foreach" also works and i belive same should hold good for JAVA script also.

could yu suggest if there any action element to check configured memory and vcpu size of vms .

0 Kudos
iiliev
VMware Employee
VMware Employee

Perhaps you have Array#forEach in mind? In this case, the code could look like:

var allVMs = VcPlugin.getAllVirtualMachines();

allVMs.forEach(function(vm) {

  System.log("vmname: " + vm.name);

})

As for vcpu and memory, their values can be retrieved using vm.config.hardware.numCPU and vm.config.hardware.memoryMB, respectively.

0 Kudos
jvm2016
Hot Shot
Hot Shot

i added few more lines to find mem and cpu of each vm and using api explorer i was able to find .

can yu suggest any way to outthis to text or csv file as we do in powershell.

also for each is correct as yu suggested not sure how foreach works earlier.

0 Kudos
jvm2016
Hot Shot
Hot Shot

pastedImage_0.png

i added as above .how to out this to text or csv file.

0 Kudos