exconnect.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. ####################################################################################### use strict; use warnings; use Getopt::Long; use VMware::VIRuntime; use VMware::VILib; ####################################################################################### # # exconnect.pl # Example script that connects the CD Drive to a Virtual Machine # Parameters: # -- serverserver 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(); my $vm_name = Opts::get_option ('vm'); ####################################################################################### # # Find the virtual machine # ####################################################################################### 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); ####################################################################################### # # Find the device # ####################################################################################### my $devName = 'CD/DVD Drive 1'; my $device = FindDevice ($vm_view, $devName); Fail ("Device $devName not found.\n") unless (defined ($device)); ####################################################################################### # $device now points to the Device Object for the CD-ROM # Inspect the Connectable object to verify that it's connected # Change the connected attribute to 1 (connected) # Build the VirtualDeviceConfigSpec # Reconfigure the Virtual Machine ####################################################################################### if ($device->connectable->connected == 0) { print "Preparing to connect ", $device->deviceInfo->label, " on Virtual Machine $vm_name\n"; $device->connectable->connected(1); my $devspec = VirtualDeviceConfigSpec->new( device => $device, operation => VirtualDeviceConfigSpecOperation->new('edit') ); my $vmspec = VirtualMachineConfigSpec->new( deviceChange => [$devspec] ); print "Reconfiguring Virtual Machine\n"; eval { $vm_view->ReconfigVM( spec => $vmspec ); }; if ($@) { print "Reconfiguration failed.\n $@"; } else { print "Device connected.\n"; } } else { print "Device $devName is already connected.\n"; } # logout Util::disconnect(); ####################################################################################### # # FindDevice # Arguments: # $vm -> Pointer to vm view # $name -> String of device to be found # # Returns: # Pointer to Device Object matching the name # undef if device not found. # Iterates through array under device is found # ####################################################################################### sub FindDevice { my ($vm, $name) = @_; my $devices = $vm->config->hardware->device; foreach my $device (@$devices) { return $device if ($device->deviceInfo->label eq $name); } return undef; } 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.