The last step is to close the server connection.

To log out and exit, use the Util::disconnect() subroutine. Sample Script (Commented Version) shows the complete listing for simpleclient.pl.

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

  1. Open a command prompt or console.
  2. Change to the directory that contains the simpleclient.pl script.
  3. Run the script by using the following syntax.
    perl simpleclient.pl <conn_params> --entity <EntityType>

    For example:

    perl simpleclient.pl --server aquarium.mycomp.com --username abalone --password tank --entity HostSystem
    Found HostSystem: abcd-42.shellfish.vmware.com