6.5

Table Of Contents
vSphere SDK for Perl Programming Guide
20 VMware, Inc.
Step 1: Import the vSphere SDK for Perl Modules
AllvSphereSDKforPerlscriptsmustusetheVMware::VIRuntimemodule:
use VMware::VIRuntime;
Thismodulehandlesallclientsideinfrastructuredetails.Forexample,ittransparentlymapsdatatypesand
provideslocalPerlinterfacestoserversideobjects.Themodulealsoloadssubroutinesthatyoucanuseto
connecttoavCenterServerorESX/ESXisystemandtoretrieveviews.Viewsaretheclientside
Perlobjects
thatencapsulatethepropertiesandoperationsofserversidemanagedobjects.Thesubroutinesareorganized
intodifferentpackages:
TheOptspackagesubroutineshandlebuiltinoptionsandcreatingcustomoptions.
TheUtilpackagesubroutinesfacilitateroutinetasks,suchassettingupandclosingconnectionstothe
server.
TheVimpackagesubroutinesaccessserversidemanagedobjects,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.
Anumberofcommoncommandlineoptions,mostofthemconnectionoptions,arealreadydefinedforall
utilityapplications(seeTable 13,“OptionsAvailableforAllSDKforPerlCommands,onpage 16).In
addition,mostapplicationshaveapplicationspecificoptionsyoupass
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)DefineScriptSpecific
CommandLineOptions”onpage 20.
Util::connect();
“Step3:ConnecttotheServeronpage 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:ObtainViewObjectsofServerSide
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