VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

javascript eqivalent to powercli_vRO

Hi llian,

could you tell what is the equivalent in java script for the folowing line .

$x= get-vm|where-object{$_.powerstate -eq "poweredoff}

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

.poweredon also need to be .poweredOn

View solution in original post

11 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

It looks like this PowerCLI script gets list of VMs that are powered off.

An equivalent in vRO Javascript could be something like the following:

// Get list of powered-off VMs

var poweredOffVMs = VcPlugin.getAllVirtualMachines().filter(function(vm) {

  return vm.runtime.powerState == VcVirtualMachinePowerState.poweredOff;

});

// Print their names

for each (vm in poweredOffVMs) {

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

}

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks.I am checking this.

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

hi llian ,

the code which u mentioned prints the list of all vms in vcenter not the poweredoff .

also could you tell me what is the use of "return"  in this code/

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

OK, let's got over the code step by step:

VcPlugin.getAllVirtualMachines() returns an array with all virtual machines.

filter(somefunction) applies the boolean function somefuction to all elements of the array one by one, and returns a new array with those elements from the original array for which the function evaluates to true

return vm.runtime.powerState == VcVirtualMachinePowerState.poweredOff; is an expression that returns true or false depending on whether the powerState property has value poweredOff or not.

Combining the above, the code on lines 1..4 returns those virtual machines that has their powerState property set to poweredOff

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks .i am checking again why it is printing the entire list of vms in Vcenter .

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Could you check what happens if you replace poweredOff with poweredOn (to get the list of powered on vms)?

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Hi llian,

good morning

i did to find powered on vms as per following code

pastedImage_1.png

but got folliwing error ;

pastedImage_2.png

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

The code contains a typo; the method getAllVirtualMAchines() should be getAllVirtualMachines with lowercase 'a' in word Machines

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

it has run successfully but for some resons not printing the names. though we have

System.log ........

i am doing this on vmware hands on lab  hope that does not make any difference .

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

.poweredon also need to be .poweredOn

jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks .I need to remeber again that java script is case sensitive.

it worked fine .

Reply
0 Kudos