VMware {code} Community
billguy
Contributor
Contributor
Jump to solution

How to list vm's and their respective hosts within a resource pool

Looked through the perl toolkit programming guide but nothing clicked yet.

I guess a "VI PHP toolkit" is out of the question so I'm forced to learn Perl (which probably isn't a bad idea anyway, even if it is second best next to PHP). Smiley Wink (lighten up, just instigating).

Now that some of you are shaking in anger, I'll ask my question:

How can I get a list of VM's and the hosts they are running on within a resource pool using the perl toolkit?

(vidiscovery.pl gets me close but not exactly what I need).

0 Kudos
1 Solution

Accepted Solutions
StefanPahrmann
Jump to solution

Hi,

following will do (put the right values in the first 4 lines):

my $filter=<ressourcegroup name>;
my $service_url="https://localhost/sdk";
my $username="";
my $password="";

use VMware::VIM2Runtime;
use VMware::VILib;

Vim::login(service_url => $service_url, user_name => $username, password => $password);
my $ressource_group = Vim::find_entity_view(view_type => 'ResourcePool', 
                                        filter => {'name' => $filter } );

my $xvm_views = Vim::find_entity_views(view_type => "VirtualMachine",
                                     begin_entity => $ressource_group );
foreach(@$xvm_views){
     $host=Vim::get_view(mo_ref => ($_->runtime->host) );
     print "VM: ".$_->name;
     print " Host:".$host->name."\n";
}

Vim::logout();

Regards

Stefan

Message was edited by: StefanPahrmann (forgot the 'use'-statements)

View solution in original post

0 Kudos
3 Replies
StefanPahrmann
Jump to solution

Hi,

following will do (put the right values in the first 4 lines):

my $filter=<ressourcegroup name>;
my $service_url="https://localhost/sdk";
my $username="";
my $password="";

use VMware::VIM2Runtime;
use VMware::VILib;

Vim::login(service_url => $service_url, user_name => $username, password => $password);
my $ressource_group = Vim::find_entity_view(view_type => 'ResourcePool', 
                                        filter => {'name' => $filter } );

my $xvm_views = Vim::find_entity_views(view_type => "VirtualMachine",
                                     begin_entity => $ressource_group );
foreach(@$xvm_views){
     $host=Vim::get_view(mo_ref => ($_->runtime->host) );
     print "VM: ".$_->name;
     print " Host:".$host->name."\n";
}

Vim::logout();

Regards

Stefan

Message was edited by: StefanPahrmann (forgot the 'use'-statements)

0 Kudos
billguy
Contributor
Contributor
Jump to solution

Perfect. Thanks Stefan.

FWIW, Learning Perl, has been a big help.

0 Kudos
StefanPahrmann
Jump to solution

You're welcome, glad i could help Smiley Happy

Btw I would appreciate if you marked my answer as a correct answer.

Regards

Stefan

0 Kudos