#!/usr/bin/perl -w # # This is a sample script and is provided as an example only. # This script is provided as is and is no support is provided. # # This script prints names of all VM's with Windows guest use strict; use warnings; use Getopt::Long; use VMware::VIM2Runtime; use VMware::VILib; use VMware::VIRuntime; $Util::script_version = "1.0"; my %opts = ( 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 $datacenter_view = Vim::find_entity_view(view_type => 'Datacenter', filter => { name => $datacenter }); if (!$datacenter_view) { die "Datacenter '" . $datacenter . "' not found\n"; } # get all hosts under this datacenter my $host_views = Vim::find_entity_views(view_type => 'HostSystem', begin_entity => $datacenter_view); # print hosts my $counter = 1; print "Hosts found:\n"; foreach my $host (@$host_views) { print "$counter: " . $host->name . "\n"; $counter++; } # print vm's $counter = 1; print "\nVM's found:\n"; # get all VM's under this datacenter my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $datacenter_view); ####################################################################################### # Iterate through each Virtual Machine ####################################################################################### foreach my $vm_view (sort {$a->name cmp $b->name} @$vm_views) { my $vm_name = $vm_view->name; print "$counter: " . $vm_name . "\n"; # # print information about each virtual machine disk ... if tools is installed then VirtualMachine.guest.disk[] will contain the array of disks # my $disks = $vm_view->guest->disk; if (defined $disks) { print "VM Disks\n"; foreach my $disk (@$disks) { my $capacity = $disk->capacity / (1024 * 1024 * 1024); my $used = (1 - ($disk->freeSpace / $disk->capacity)) * 100; printf (" %s: Capacity %.1fGB; Used %.1f%%\n", $disk->diskPath, $capacity, $used); } } # # skip any virtual machine that has incomplete information # if (! defined $vm_view->layout) { $counter++; next; } 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"; } } $counter++; } # disconnect from the server Util::disconnect(); sub Fail { my ($msg) = @_; Util::disconnect(); die ($msg); exit (); }