VMware {code} Community
ichuckwow
Contributor
Contributor
Jump to solution

Retrieving power status from Perl API

I was reading over some of the already generated perl scripts such as vminfo.pl, although it does not tell me the power status of a VM.

How can I retrieve the power status of a VM? Whether it be on/off/suspended etc.

I've searched around the forum here a little bit and have not found an answer to this yet.

0 Kudos
1 Solution

Accepted Solutions
rcardona2k
Immortal
Immortal
Jump to solution

Try something like:

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

foreach (@$vms) {

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

}

View solution in original post

0 Kudos
4 Replies
rcardona2k
Immortal
Immortal
Jump to solution

Try something like:

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

foreach (@$vms) {

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

}

0 Kudos
ichuckwow
Contributor
Contributor
Jump to solution

Ah, This works perfect!

Output is:

VM: Demo4, powerstate: poweredOn

Great! Thanks!

0 Kudos
ichuckwow
Contributor
Contributor
Jump to solution

Quick question...

Is there a way to setup "options" for this file similar to the vm apps files?

e.g powerstatus.pl --vm = somevm

0 Kudos
njain
Expert
Expert
Jump to solution

You can indeed setup the command line options similar to other vm apps files by declaring the option as a hash, adding the option to the default options and then parsing and validating the options before connecting to the server:

my %opts = (

entity => {

type => "=s",

variable => "vmname",

help => "Virtual Machine name",

required => 1,

},

);

Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Please refer to "Writing VI Perl Toolkit Scripts" section of VI Perl Toolkit Programming Guide for details:

http://www.vmware.com/support/developer/viperltoolkit/viperl16/doc/viperl_proggd.pdf

0 Kudos