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
vSphere SDK for Perl Programming Guide
20 VMware, Inc.
Step 1: Import the vSphere SDK for Perl Modules
AllvSphereSDKforPerlscriptsmustusetheVMware::VIRuntimemodule:
use VMware::VIRuntime;
Thismodulehandlesallclient‐sideinfrastructuredetails.Forexample,ittransparentlymapsdatatypesand
provideslocalPerlinterfacestoserver‐sideobjects.Themodulealsoloadssubroutinesthatyoucanuseto
connecttoavCenterServerorESX/ESXisystemandtoretrieveviews.Viewsaretheclient‐side
Perlobjects
thatencapsulatethepropertiesandoperationsofserver‐sidemanagedobjects.Thesubroutinesareorganized
intodifferentpackages:
TheOptspackagesubroutineshandlebuilt‐inoptionsandcreatingcustomoptions.
TheUtilpackagesubroutinesfacilitateroutinetasks,suchassettingupandclosingconnectionstothe
server.
TheVimpackagesubroutinesaccessserver‐sidemanagedobjects,instantiatelocalproxyobjects(views),
updateproperties,andrunlocalmethodsthatresultinoperationsonremoteservers.
See“vSphereSDKforPerlSubroutineReference”onpage 41.
Step 2: (Optional) Define Script-Specific Command-Line Options
Whenyourunascriptfromthecommandline,youusuallyspecifyconnectioninformationandmightalso
specifyotherinformationsuchasavirtualmachinethatyouwanttopowerofforahostforwhichyouneed
statusinformation.vSphereSDKforPerlletsyouspecifytheseoptionsina
varietyofways.See“Specifying
Options”onpage 12.
Anumberofcommoncommand‐lineoptions,mostofthemconnectionoptions,arealreadydefinedforall
utilityapplications(seeTable 1‐3,“OptionsAvailableforAllSDKforPerlCommands,”onpage 16).In
addition,mostapplicationshaveapplication‐specificoptionsyoupass
tothescriptatexecutiontime.
my %opts = (
entity => {
type => "=s",
variable => "VI_ENTITY",
help => "ManagedEntity type: HostSystem, etc",
required => 1,
},
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
“Step2:(Optional)DefineScript‐Specific
Command‐LineOptions”onpage 20.
Util::connect();
“Step3:ConnecttotheServer”onpage 22.
# Obtain all inventory objects of the specified type
my $entity_type = Opts::get_option('entity');
my $entity_views = Vim::find_entity_views(
view_type => $entity_type);
“Step4:ObtainViewObjectsofServer‐Side
ManagedObjects”onpage 22.
# Process the findings and output to the console
foreach my $entity_view (@$entity_views) {
my $entity_name = $entity_view->name;
Util::trace(0, "Found $entity_type:
$entity_name\n");
}
“Step5:ProcessViewsandReportResults”
onpage 22.
# Disconnect from the server
Util::disconnect();
“Step6:ClosetheServerConnection”on
page 23.
Table 2-1. Basic vSphere SDK for Perl Script (simpleclient.pl) (Continued)
Code element Discussed in