VMware
7 Replies Last post: Jun 26, 2009 2:41 PM by stumpr  

Trouble retrieving VM objects posted: Jun 25, 2009 7:11 PM

Click to view rsantos@qualcomm.com's profile Lurker 4 posts since
Oct 16, 2007
I'm trying to collect all VMs in a datastore from a given datacenter, passed as a command line argument. Here's a snippet:

my %allvms = ();

my $clusters = Vim::find_entity_views(
view_type => 'ClusterComputeResource',
filter => { 'name' => "$cluster_name" },
);

foreach my $cluster ( @$clusters ){
print "Cluster: " . $cluster->name . "\n";
my $datastores = Vim::get_views (mo_ref_array => $cluster->datastore);
foreach my $datastore (sort @$datastores){
my $vms = Vim::get_views (mo_ref_array => $datastore->vm);
foreach my $vm ( @$vms ){
$allvms{$vm->name}{path} = $vm->summary->config->vmPathName;
$allvms{$vm->name}{name} = $vm->name;
}
}
}


foreach ( sort keys %allvms ){ print "$_: $allvms{$_}{path}\n"; }

But the %allvms hash is empy. What am I doing wrong?

Thanks much!
Rob

Re: Trouble retrieving VM objects

1. Jun 26, 2009 9:21 AM in response to: rsantos@qualc…
Click to view stumpr's profile Expert 451 posts since
Sep 26, 2007
Have you looked at the named parameter "begin_entity" ?

You could probably save some logic by getting a ManagedObject Reference to your datacenter of choice, then make your find_entity_views call:

my $virtual_machine_views = Vim::find_entity_view(
     view_type => "VirtualMachine",
     begin_entity => $datacenter_moref,
}


That would probably be a little easier.

Re: Trouble retrieving VM objects

3. Jun 26, 2009 1:46 PM in response to: rsantos@qualc…
Click to view stumpr's profile Expert 451 posts since
Sep 26, 2007
Sorry, didn't read that detail the first time. That's also easy.

Look at the Datastore object, particular the vm property. It'll give you an array of morefs of VMs using it. Obviously, this won't include VMs that are not in the VC inventory, for that you'd have to search the datastore for files and make some assumptions about their validity by name.

Re: Trouble retrieving VM objects

5. Jun 26, 2009 2:14 PM in response to: rsantos@qualc…
Click to view stumpr's profile Expert 451 posts since
Sep 26, 2007
So I wrote some code almost matching what you got (only difference was variable names). It's working for me.

Is your hash declaration in a loop so it's getting reset?

Re: Trouble retrieving VM objects

7. Jun 26, 2009 2:41 PM in response to: rsantos@qualc…
Click to view stumpr's profile Expert 451 posts since
Sep 26, 2007
At least it was something simple. Sorry I misread your post not once, but twice. ;)

Developer Social Media

Communities