jasnyder
Hot Shot
Hot Shot

When you say use vCLI to do this, are you OK with using the perl modules that come with the vCLI package?

If so, then I wrote a perl script to add a serial device to a VM, which you can use.

Put these lines into a new file called add-serial.pl in the vmware-vsphere-cli-distrib/apps/vm folder wherever you unpacked the vCLI package (for instance ~/Downloads/vmware-vsphere-cli-distrib/apps/vm).  chmod +x the file after saving.

#!/usr/bin/perl -w

use strict;

use warnings;

use FindBin;

use lib "$FindBin::Bin/../";

use VMware::VIRuntime;

use AppUtil::HostUtil;

use AppUtil::VMUtil;

$Util::script_version = "1.0";

my %opts = (

   vm => {

      type => "=s",

      help => "The VM name to perform the operation on",

      required => 1,

      default => "",

   }

);

Opts::add_options(%opts);

Opts::parse();

my $vmName = Opts::get_option('vm');

my %filterhash = ();

Util::connect();

my $vm_views = VMUtils::get_vms('VirtualMachine',

                                   $vmName,

                                   undef,

                                   undef,

                                   undef,

                                   undef,

                                   %filterhash);

my $vm_view = shift (@$vm_views);

if($vm_view) {

      my $serial;

   

      my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('add');

      my $serial_backing_info

         = VirtualSerialPortURIBackingInfo->new(direction => "server",

                        serviceURI => "telnet://:7003");

    my $connectInfo = VirtualDeviceConnectInfo->new(allowGuestControl => 1,

                            connected => 1,

                            startConnected => 1);

      $serial = VirtualSerialPort->new(key => -1,

                                  backing => $serial_backing_info,

                   connectable => $connectInfo,

                   yieldOnPoll => 1);

    my $devspec = VirtualDeviceConfigSpec->new(operation => $config_spec_operation,

                                                 device => $serial);

    my @devspecs = ( $devspec );

    my $vmspec = VirtualMachineConfigSpec->new(deviceChange => \@devspecs);

    $vm_view->ReconfigVM( spec => $vmspec );

}

Util::disconnect();

This is run using ./add-serial.pl --server esx_server_name --username root --password root_password -vm vm_name

View solution in original post