#!/usr/bin/perl -w # # Script provided as a sample. # DISCLAIMER. THIS SCRIPT IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, # WHETHER ORAL OR WRITTEN, EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES # OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. ################################################################################### use strict; use warnings; use VMware::VIM2Runtime; use VMware::VILib; my %opts = ( datastore => { type => "=s", help => "Datastore name", required => 1}, datacenter => { type => "=s", help => "Datacenter name", required => 1}); # read/validate options and connect to the server Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); # find datacenter my $datacenter = Opts::get_option('datacenter'); my $datastore = Opts::get_option ('datastore'); my $vm_views = LookupDataStore ($datacenter, $datastore); die ("No match for datastore $datastore in datacenter $datacenter.\n") unless ($vm_views); print "Virtual Machines using datastore $datastore:\n"; foreach my $vm_view (@$vm_views) { print $vm_view->name, "\n"; } # disconnect from the server Util::disconnect(); ####################################################################################################### # LookupDataStore # Arguments: # datacentername => name of the datacenter for root of search # datastorename => name of datastore # Returns: # list of Virtual Machine views which use that datastore # 0 if none # # Find the datacenter that matches the name # The datacenter object contains the list of datastores contained in it # Select the matching datastore # List the Virtual Machines stores in the "vm" property of the datastore ####################################################################################################### sub LookupDataStore { my ($datacenter_name, $datastore_name) = @_; my $datacenter_view = Vim::find_entity_view(view_type => 'Datacenter', filter => {'name' => "^$datacenter_name\$"}); return 0 unless ($datacenter_view); my $datastore_view = SelectDatastore ($datacenter_view->datastore, $datastore_name); return 0 unless ($datastore_view); my $vm_views = Vim::get_views (mo_ref_array => $datastore_view->vm); return $vm_views; } ####################################################################################################### # SelectDatastore # Arguments: # datastores -> list of datastore morefs # name -> name of datastore to match # Returns: # datastore_view that matches the requested name # 0 if none was found ###################################################################################################### sub SelectDatastore { my ($datastores, $datastore_name) = @_; my $datastore_views = Vim::get_views (mo_ref_array => $datastores); foreach my $datastore_view (@$datastore_views) { return $datastore_view if ($datastore_view ->info->name eq $datastore_name); } return 0; }