VMware Virtual Machine Reconfiguration Webinar Sample Code

VMware Virtual Machine Reconfiguration Webinar Sample Code

exlisthw.pl

Copyright © 2008 VMware, Inc. All rights reserved.

#!/usr/bin/perl -w

#######################################################################################
# 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. 
#######################################################################################

#######################################################################################
#
# exlisthw.pl
#	Example script that lists the virtual hardware of a virtual machine
#	Parameters:
#		-- server 	server name (either VC or ESX dns or ip address)
#		-- username 	credentials
#		-- password 		
#		-- vm  		name of virtual machine to query
#######################################################################################

use strict;
use warnings;
use Getopt::Long;
use VMware::VIRuntime;
use VMware::VILib;

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);

#######################################################################################
#
# The config->hardware data object includes the virtual hardware information
#	numCPU     =>    number of virtual CPUs
#	memoryMB   =>    memory 
#	devices[]  =>    array of devices configured in virtual machine
#
#######################################################################################
my $virtual_hardware = $vm_view->config->hardware;
print "Information for Virtual Machine:  $vm_name\n";
printf ("Number of CPUs:       %d\n", $virtual_hardware->numCPU);
printf ("Memory Capacity (MB): %d\n", $virtual_hardware->memoryMB);
print "Summary of Devices\n";
print "Device         Key  Type\n"; 
print "------------------------ \n";

my $devices = $virtual_hardware->device;
foreach my $dev (sort {$a->key <=> $b->key} @$devices) {
    printf ("%-15.15s %4d %-20.20s\n", $dev->deviceInfo->label, $dev->key, ref($dev));
    }

# logout
Util::disconnect();


sub Fail {
    my ($msg) = @_;
    Util::disconnect();
    die ($msg);
    exit ();
}

The sample code is provided "AS-IS" for use, modification, and redistribution in source and binary forms, provided that the copyright notice and this following list of conditions are retained and/or reproduced in your distribution. To the maximum extent permitted by law, VMware, Inc., its subsidiaries and affiliates hereby disclaim all express, implied and/or statutory warranties, including duties or conditions of merchantability, fitness for a particular purpose, and non-infringement of intellectual property rights. IN NO EVENT WILL VMWARE, ITS SUBSIDIARIES OR AFFILIATES BE LIABLE TO ANY OTHER PARTY FOR THE COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, INDIRECT, OR SPECIAL DAMAGES, ARISING OUT OF THIS OR ANY OTHER AGREEMENT RELATING TO THE SAMPLE CODE.

You agree to defend, indemnify and hold harmless VMware, and any of its directors, officers, employees, agents, affiliates, or subsidiaries from and against all losses, damages, costs and liabilities arising from your use, modification and distribution of the sample code.

VMware does not certify or endorse your use of the sample code, nor is any support or other service provided in connection with the sample code.