vSphere

 View Only
  • 1.  Retrieve Power status

    Posted Sep 27, 2012 02:35 PM

    Hi there,

    I am currently using the marvelous listVMByFolder script (http://communities.vmware.com/docs/DOC-10059), and I would like to know how I can retrieve the power status of each VM I list. I have been using the Perl SDK for about 2 days (yeah, it is not a lot), and while I understand how I would do "normally", I do not figure out how to retrieve this information while being in such a statement :

    if ( $child_view->isa("VirtualMachine") )
    {
           # Some code here
    }

    How can I retrieve this information since I am not really working on a VM object, like I would find in any tutorial out there, like something like this :


    my $vms = Vim::find_entity_views(view_type => 'VirtualMachine');

    foreach (@$vms) 

    {

        print "VM: " . $_->config->name . ", powerstate: " . $_->runtime->powerState->val . "\n";

    }

    Thank you very much and have a nice day ! :smileyhappy:



  • 2.  RE: Retrieve Power status

    Posted Sep 28, 2012 04:17 AM

    That's an old one I wrote :smileywink:

    In that script, the generic child_view call is just a cheap, fast way to get the name and object type.  I cheat a bit and use a common property 'name' in the Vim:get_view() call to do a the printing on both folders and VMs.

    But if you wanted the VM power state, you would have to get the power state property as well.  Not the most efficient way of getting the data, but should work without major script rework (should work not tested, but idea is sound) --

    foreach $mo ( @{$entity_view->childEntity} )
                    {
                            $child_view = Vim::get_view(
                                    mo_ref => $mo, properties => ['name']
                            );
                            if ( $child_view->isa("VirtualMachine") )
                            {
                                  $vm_view = Vim::get_view(mo_ref => $mo, properties => [ 'name', 'runtime.powerstate' ]);
                                  print " " x $index . "Virtual Machine: " . $child_view->name . "(PowerState=)" . $vm_view->{'runtime.powerstate'}->val . "\n" ;
                            }
                            if ( $child_view->isa("Folder") )
                            {
                                    print " " x $index . "Folder: " . $child_view->name . "\n";
                                    $child_view = Vim::get_view(
                                            mo_ref => $mo, properties => ['name', 'childEntity']
                                    );
                                    TraverseFolder($mo, $index);
                            }
                    }

    You could also check the MOREF type and have an if around the call to get the $child_view (if VirtualMachine, then call get_view with the runtime.powerstate, if not, just call 'name').



  • 3.  RE: Retrieve Power status

    Posted Sep 28, 2012 07:30 AM

    EDIT : It perfectly works, thanks :smileygrin:

    PS : The name of the property is runtime.powerState or an InvalidPropertyFault is raised :smileywink:



  • 4.  RE: Retrieve Power status
    Best Answer

    Posted Sep 28, 2012 02:22 PM

    Was just responding when I saw you caught the spelling mistake :smileyhappy: