Hi,
I have multiple Datacenters in the vCetner and I want to list VMs only in one perticular Datacenter. Below is the script I have written. Apologies, I am an amateur and my script is a mess;
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use VMware::VIRuntime;
my %opts = (
datacenter => {
type => "=s",
help => "Please enter the Datacenter name",
required=> 1,
}
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Util::connect();
my $dc = Opts::get_option('datacenter');
my $dc_views = Vim::find_entity_views ( view_type => "Datacenter",
filter => { name => $dc } );
foreach (@$dc_views)
{
my $funky = Vim::get_view ( mo_ref => $_->vmFolder);
my $funky2 = $funky->childEntity ;
my $funky3 = Vim::get_views (mo_ref_array => $funky2);
foreach (@$funky3) { print $_->name . "\n"; }
}
when I run above the out put just says "Discovered virtual machine":
#perl test.pl --server <whatever> --username <whatever> --password <whatever> --datacenter DC01_ESXi5
There is a slightly easier way than traversing the inventory tree.
If you just want entities under a datacenter, you can use begin_entity in your find_entity_view* calls.
#!/usr/bin/perl -wuse strict;use Data::Dumper;use VMware::VIRuntime;my %opts = (datacenter => {type => "=s",help => "Please enter the Datacenter name",required => 1,});Opts::add_options(%opts);Opts::parse();Opts::validate();Util::connect();my $dc = Opts::get_option('datacenter');my $dc_view = Vim::find_entity_view ( view_type => "Datacenter",filter => { name => $dc },properties => [ 'name' ] );die "No Datacenter named '$dc' found!" unless $dc_view;my $vm_views = Vim::find_entity_views( view_type => 'VirtualMachine', begin_entity => $dc_view );foreach my $vm (@$vm_views) {print "vm: " . $vm->name . "\n";}
A Folder is a recursive container: it can contain other Folders.
The "vmFolder" of a Datacenter can contain VirtualMachines and/or other Folders. That's what you're probably seeing, a Folder called "Discovered virtual machines".
There's a Perl SDK sample called "listVMsByFolder.pl" that should help:
Sure. I will take a look at hte script you mentioned. I think I need to understand the vSphere perl API reference documentation. Being not a developer, I find it very difficult to interpret hte documentation on perl SDK. Thanks BenN.
There is a slightly easier way than traversing the inventory tree.
If you just want entities under a datacenter, you can use begin_entity in your find_entity_view* calls.
#!/usr/bin/perl -wuse strict;use Data::Dumper;use VMware::VIRuntime;my %opts = (datacenter => {type => "=s",help => "Please enter the Datacenter name",required => 1,});Opts::add_options(%opts);Opts::parse();Opts::validate();Util::connect();my $dc = Opts::get_option('datacenter');my $dc_view = Vim::find_entity_view ( view_type => "Datacenter",filter => { name => $dc },properties => [ 'name' ] );die "No Datacenter named '$dc' found!" unless $dc_view;my $vm_views = Vim::find_entity_views( view_type => 'VirtualMachine', begin_entity => $dc_view );foreach my $vm (@$vm_views) {print "vm: " . $vm->name . "\n";}