#!/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 Getopt::Long; use VMware::VIRuntime; use VMware::VILib; ####################################################################################### # # exfiles.pl # Example script to list files for a VM. # Parameters: # -- server server name (either VC or ESX dns or ip address) # -- username credentials # -- password # -- vm name of virtual machine to query ####################################################################################### my %opts = ( vm => { type => "=s", variable => "vmName", help => "Virtual Machine Name", required => 1}); # validate options, and connect to the server Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); ####################################################################################### # # Find the Virtual Machine ####################################################################################### my $vm_name = Opts::get_option ('vm'); my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => "^$vm_name\$"}); Fail ("Virtual Machine $vm_name was not found.\n") unless ($vm_view); my $files = $vm_view->layout; foreach my $file ('configFile', 'disk', 'logFile', 'snapshot', 'swapFile') { my $item = $files->$file; next unless (defined $item); print "$file\n"; # # for snapshots, list the snapshot name, and the files of each snapshot # if ($file eq 'snapshot') { foreach my $snapshot (@$item) { my $i = 0; my $list = $snapshot->snapshotFile; my $name = $snapshot->key->value; print " $name", "\n"; foreach my $element (@{$list}) { print " [$i] ", $element, "\n"; $i++; } } } # # for disk files, list the diskFile array elsif ($file eq 'disk') { my $i = 0; foreach my $disk (@{$item}) { foreach my $element (@{$disk->diskFile}) { print " [$i]: ", $element, "\n"; $i++; } } } # # the item may have an array of files ... elsif (ref($item) eq 'ARRAY') { my $i = 0; foreach my $element (@{$item}) { print " [$i]: ", $element, "\n"; $i++; } } # the item is a simple item else { print " ", $item, "\n"; } } # logout Util::disconnect(); sub Fail { my ($msg) = @_; Util::disconnect(); die ($msg); exit (); }