VMware {code} Community
dfaul123
Contributor
Contributor

Problem looking up VMs per datastore when using Custom Folders.

I'm currently using the following subroutine to select VMs per datastore. This routine seems to fail if you have a custom folder in the object tree. For example: Having a folder like CompanyX above the DataCenter objects.

Is there anyway to build a subroutine that would allow finding VMs per datacenter/datastore? This way I could add a datacenter argument to the script allowing me to first search for datacenter X, then datastore y and output only the VMs in datastore y.

Thank you much!

sub lookupDatastore {

my ($name) = @_;

my $service_view = Vim::get_service_content();

my $view_rootFolder = Vim::get_view(mo_ref => $service_view->rootFolder); # start here

foreach my $dataCenter (@\{$view_rootFolder->childEntity}) {

my $datastores = Vim::get_view (mo_ref => $dataCenter);

foreach (@\{$datastores->datastore}) {

my $datastore = Vim::get_view (mo_ref => $_);

return $datastore if ($datastore->info->name eq $name);

}

}

return 0;

}

0 Kudos
4 Replies
hrobinson
VMware Employee
VMware Employee

For finding VMs in in a datacenter, that's very straightforward since VirtualMachines are directly linked to a datacenter. I'm attaching a snippet of the relevant code in /samples/discovery/datacenter.pl which is on the perl distribution. I'll explain the VMs in a datastore in the next thread.

\# find datacenter

my $datacenter = Opts::get_option('datacenter');

my $datacenter_view = Vim::find_entity_view(view_type => 'Datacenter',

filter => \{ name => $datacenter });

if (!$datacenter_view) {

die "Datacenter '" . $datacenter . "' not found\n";

}

\# get all VM's under this datacenter

my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine',

begin_entity => $datacenter_view);

foreach (@$vm_views) {

print "$counter: " . $_->name . "\n";

$counter++;

}

0 Kudos
hrobinson
VMware Employee
VMware Employee

There's a faster way to iterate through the datacenters.

You can use

Vim::find_entity_views (view_type => 'DataCenter') and that will provide you will all of the datacenters. This should fix your problem.

Unfortunately, you can't do the same to datastore (as they aren't considered an entity).

To relate the VM's to a datastore, then you would have to find all the VMs (example provided above), and then for each VM, see if it references the datastore.

H

0 Kudos
hrobinson
VMware Employee
VMware Employee

I've included a sample script that includes how I would do it. Once you have found the datastore, then there's a property that provides you the list of vms that use it.

By using find_entity_views, you automatically find objects that match that list. By using find_entity_views instead of starting from the root folder, you don't have the have to trace around custom folders.

Hold this helps.

(Note: A copy of this script has been posted at http://communities.vmware.com/docs/DOC-1133?tstart=0 . --Harvey)

0 Kudos
dfaul123
Contributor
Contributor

Thanks so much Henry. This is exactly what I needed!

0 Kudos