VMware {code} Community
slayedbylucifer
Enthusiast
Enthusiast
Jump to solution

List all the VMs in a Datacenter

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

Discovered virtual machine
It would be great if someone could explain where i am going wrong. I don't want a script which would get me the intended result, indeed, I want to know where i am going wrong in above script. I am finding it extremely difficult to understand the vSphere API data structure. Perl programming is not the issue here.
Thnaks.
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
stumpr
Virtuoso
Virtuoso
Jump to solution

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 -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_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";
}
Reuben Stump | http://www.virtuin.com | @ReubenStump

View solution in original post

0 Kudos
3 Replies
BenN
Enthusiast
Enthusiast
Jump to solution

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:

    http://communities.vmware.com/docs/DOC-10059

slayedbylucifer
Enthusiast
Enthusiast
Jump to solution

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.

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

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 -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_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";
}
Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos