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
VMware, Inc. 23
Chapter 2 Writing vSphere SDK for Perl Scripts
See“UnderstandingServer‐SideObjects”onpage 24.
Step 6: Close the Server Connection
Tologoutandexit,usetheUtil::disconnect()subroutine.Example 2‐1showsthecompletelistingfor
simpleclient.pl.
Example 2-1. Sample Script (Commented Version)
#!/usr/bin/perl
# The simpleclient.pl script outputs a list of all the entities of the specified managed-entity
# type (ClusterComputeResource, ComputeResource, Datacenter, Datastore, Folder, HostSystem,
# Network, ResourcePool, VirtualMachine, or VirtualService) found on the target vCenter Server or
# ESX system. Script users must provide logon credentials and the managed entity type. The script
# leverages the Util::trace() subroutine to display the found entities of the specified type.
use strict;
use warnings;
use VMware::VIRuntime;
# Defining attributes for a required option named 'entity' that
# accepts a string.
#
my %opts = (
entity => {
type => "=s",
variable => "VI_ENTITY",
help => "ManagedEntity type: HostSystem, etc",
required => 1,
},
);
Opts::add_options(%opts);
# Parse all connection options (both built-in and custom), and then
# connect to the server
Opts::parse();
Opts::validate();
Util::connect();
# 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);
# 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");
}
# Disconnect from the server
Util::disconnect();
To run the simpleclient.pl script
1Openacommandpromptorconsole.
2 Changetothedirectorythatcontainsthesimpleclient.plscript.
3Runthescriptusingthefollowingsyntax:
perl simpleclient.pl <conn_params> --entity <EntityType>
Forexample:
perl simpleclient.pl --server aquarium.mycomp.com --username abalone --password tank --entity
HostSystem