VMware {code} Community
Nz3k
Contributor
Contributor
Jump to solution

Retrieve Power status

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 ! Smiley Happy

Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
stumpr
Virtuoso
Virtuoso
Jump to solution

Was just responding when I saw you caught the spelling mistake Smiley Happy

Reuben Stump | http://www.virtuin.com | @ReubenStump

View solution in original post

Reply
0 Kudos
3 Replies
stumpr
Virtuoso
Virtuoso
Jump to solution

That's an old one I wrote Smiley Wink

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').

Reuben Stump | http://www.virtuin.com | @ReubenStump
Nz3k
Contributor
Contributor
Jump to solution

EDIT : It perfectly works, thanks :smileygrin:

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

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

Was just responding when I saw you caught the spelling mistake Smiley Happy

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos