6.0
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
vSphere SDK for Perl Programming Guide
34 VMware, Inc.
Youcanalsomatchusingaregularexpressionobject,generallyknownasaqr//(quotedregularexpression)
object.Inthiscase,thevalueofthepropertymustmatchtheregularexpression.
ThefollowingfiltermatchesobjectswhosenamesbeginwithTest:
filter => { 'name' => qr/^Test/ }
filter => { 'name' => qr/^test/i } # make the match case-insensitive with the i option
Formoreinformationabouttheqr//operator,seetheperlre(perlregularexpressions)andperlopman
pagesinthestandardPerldocumentation.
ThefollowingexampleillustrateshowyoumightuseVim::find_entity_views()incombinationwitha
filter.Itprintsalistofvirtualmachineobjectswhoseguestoperatingsystemnamescontainthe
string
Windows.
Example 3-1. Filter that Creates Views of Windows-Based Virtual Machines Only
. . .
my $vm_views = Vim::find_entity_views(
view_type => 'VirtualMachine',
filter => {
# True if string 'Windows' appears anywhere in guestFullName
'config.guestFullName' => qr/Windows/
}
);
# Print VM names
foreach my $vm (@$vm_views) {
print "Name: " . $vm->name . "\n";
}
. . .
IfyoupassmultiplefiltercriteriatoVim::find_entity_view()orVim::find_entity_views(),the
methodreturnsonlythemanagedobjectsforwhichallcriteriamatch.Thefilterparameterspecifiedin
Example 3‐2includestwocriteria.Theexamplereturnsonlyvirtualmachinesthatfulfillbothrequirements:
GuestoperatingsystemisWindows—theconfigproperty’sguestFullNamepropertyincludesthe
stringWindows.
Virtualmachineisrunning.ThepowerstateispoweredOn.
Example 3-2. Example of Multiple Filter Specification
. . .
my $vm_views = Vim::find_entity_views(
view_type => 'VirtualMachine',
filter => {
'config.guestFullName' => qr/Windows/,
'runtime.powerState' => 'poweredOn'
}
);
# Print VM names
foreach my $vm (@$vm_views) {
print "Name: " . $vm->name . "\n";
}
. . .
Filtering Views Selectively Using Properties
EachPerlviewobjecthaspropertiesthatcorrespondtopropertiesofserver‐sidemanagedobjectsasfollows:
I
MPORTANTYoucanmatchonlypropertiesthathavesimpletypeslikestringsandnumbers.Specifyinga
propertywithacomplextypeasanargumenttoafilterresultsinafatalruntimeerror.Forexample,you
cannotspecifytheruntimepropertyofaVirtualMachineobject,whichisacomplexobject,not
astring.