jvm2016
Hot Shot
Hot Shot

javascript eqivalent to powercli_vRO

Jump to solution

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

.poweredon also need to be .poweredOn

View solution in original post

11 Replies
iiliev
VMware Employee
VMware Employee

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);

}

0 Kudos
jvm2016
Hot Shot
Hot Shot

Thanks.I am checking this.

0 Kudos
jvm2016
Hot Shot
Hot Shot

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/

0 Kudos
iiliev
VMware Employee
VMware Employee

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

0 Kudos
jvm2016
Hot Shot
Hot Shot

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

0 Kudos
iiliev
VMware Employee
VMware Employee

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

0 Kudos
jvm2016
Hot Shot
Hot Shot

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

0 Kudos
iiliev
VMware Employee
VMware Employee

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

0 Kudos
jvm2016
Hot Shot
Hot Shot

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 .

0 Kudos
iiliev
VMware Employee
VMware Employee

.poweredon also need to be .poweredOn

jvm2016
Hot Shot
Hot Shot

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

it worked fine .

0 Kudos