6.0

Table Of Contents
vSphere SDK for Perl Programming Guide
32 VMware, Inc.
view_type => 'HostSystem',
filter => { name => 'preferredHost' }
);
$vm->PowerOnVM(host => $host);
Youcannotusetheemptystringorthevalue0torepresentundeforanunsetparameter.
Updating View Objects
Inanyview,thevaluesoftheviewpropertiesrepresentthestateoftheserversideobjectsatthetimetheview
wascreated.Thesepropertyvaluesarenotupdatedautomatically.Inaproductionenvironment,thestateof
managedobjectsontheserverislikelytochangefrequently.Ifyourclient
scriptdependsontheserverbeing
inaparticularstate(poweredOnorpoweredOff,forexample),thenyoucanrefreshtheviewobject’sstate.You
canusethevSphereSDKforPerlVim::update_view_data()subroutinetorefreshthevaluesofclientside
viewswithserversidevalues.Example 23usesVim::update_view_data()
torefreshviewdata.
Example 2-3. Updating the State of View Objects
#!/usr/bin/perl
use strict;
use warnings;
use VMware::VIRuntime;
. . .
# Get all VirtualMachine objects
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine');
# Power off virtual machines.
foreach my $vm (@$vm_views) {
# Refresh the state of each view
$vm->update_view_data();
if ($vm->runtime->powerState->val eq 'poweredOn') {
$vm->PowerOffVM();
print " Stopped virtual machine: " . $vm->name . "\n";
} else {
print " Virtual machine " . $vm->name .
" power state is: " . $vm->runtime->powerState->val . "\n";
}
}