6.5
Table Of Contents
- vSphere SDK for Perl Programming Guide
- Contents
- Getting Started with vSphere SDK for Perl
- Writing vSphere SDK for Perl Scripts
- Refining vSphere SDK for Perl Scripts
- Creating and Using Filters
- Filtering Views Selectively Using Properties
- Retrieving the ServiceInstance Object on a vSphere Host
- Saving and Using Sessions
- Using Multiple Sessions
- Learning About Object Structure Using Data::Dumper
- Specifying Untyped Arguments in Scheduled Tasks and Callbacks
- Using Advanced Subroutines
- vSphere SDK for Perl Subroutine Reference
- Web Services for Management Perl Library
- Credential Store Perl Library
VMware, Inc. 29
Chapter 2 Writing vSphere SDK for Perl Scripts
Accessing Simple Property Values
Toaccessasimplepropertyfromaview,calltheproperty’saccessorontheviewobject.Theaccessorhasthe
samenameasthepropertyitself,asfollows:
$view_name->property_name
AsshowninExample 2‐1,youcanaccessthenamepropertyofentity_viewbycallingitsnamemethod,as
follows:
my $entity_name = $entity_view->name;
Accessing Enumeration Property Values
Toretrievethevalueofapropertydefinedasanenumeration,youmustdereferenceitsvaluefromwithinthe
containingobjectbyqualifyingthepropertywith->val.Forexample,thepowerstateofavirtualmachine
(powerState)isamemberoftheruntimedataobject.
Toretrievethevalueof
powerState,youmustdereferencethetwocontainingobjects(theviewobjectandthe
runtimedataobject)andthevalueitself(val),asfollows:
$vm_view->runtime->powerState->val
BecausepowerStateisanenumeration,youuseruntime->powerState->valtoretrieveitsstringvalue.
foreach my $vm (@$vm_views) {
if ($vm->runtime->powerState->val eq 'poweredOn') {
print "Virtual machine " . $vm->name . " is powered on.\n";
}
else {
print "Virtual machine " . $vm->name . " is not powered on.\n";
}
Modifying Property Values
Youcanmodifyadataobject’spropertyvaluebypassingthenewvalue,asfollows:
$data_object-> <property> (<new value>);
$data_objectisablessedreferencetoaPerlobjectorclassname,andpropertyisamethodcallontheobject.
Forexample,youcanchangetheforcepropertytofalse,asfollows:
$host_connect_spec->force ('false');
Tocreateanenumerationdataobject,useastringvalueastheargumenttotheenumerationtype’sconstructor.
my $power_state = VirtualMachinePowerState->new('poweredOff');
Typically,enumerationsareusedasargumentstomethods:
$vm->MigrateVM(
host => $target_host,
priority => VirtualMachineMovePriority->new('defaultPriority'),
state => VirtualMachinePowerState->new('poweredOff'),
Table 2-3. Property Overview
Property Example
Simpledatatype,suchasastring,Boolean,numeric,or
dateTime.
TheManagedEntitymanagedobjecthasanameproperty
oftypestring.
Arrayofsimpledatatypesordataobjects. AHostSystemmanagedobjectcontainsanarrayofvirtual
machinesthatarehostedbythecorrespondingphysical
machine.
Enumeration(enum)ofpredefined
values.Thevaluescan
beacollectionofsimpledatatypesordataobjects.
Avirtualmachine’spowerstatecanbeapoweredOn,
poweredOff,orsuspendedstringvalue.
Complexdatatypecalleddataobject(partofthevSph ere
objectmodel).
AboutInfo,Action,andServiceContentarealldata
objects.