I have shown the code below for scheduling the snapshot of a vm. I gave the name of the code as new4.pl
I executed this as
new4.pl --url http://64.104.136.204/sdk/WebService --username usrnam --password psswd --vmname vm-test --sname usingperl
i am getting an error when i execute the code. i have attched the error in this post as a picture.
please help in rectifyin the error
code:
#!/usr/bin/perl -w
#
Copyright (c) 2007 VMware, Inc. All rights reserved.
use strict;
use warnings;
use VMware::VIRuntime;
use FindBin;
use lib "$FindBin::Bin/../";
use Getopt::Long;
use VMware::VIRuntime;
use AppUtil::VMUtil;
$SIG = sub;
$Util::script_version = "1.0";
sub get_virtual_machines;
sub create_hash;
sub create_snapshots;
my %opts = (
sname => {
type => "=s",
variable => "sname",
help => "Snapshot name",
required => 1,
},
datacenter => {
type => "=s",
variable => "datacenter",
help => "Name of the datacenter",
},
pool => {
type => "=s",
variable => "pool",
help => "Name of the resource pool",
},
host => {
type => "=s",
variable => "host",
help => "Name of the host",
},
folder => {
type => "=s",
variable => "folder",
help => "Name of the folder",
},
vmname => {
type => "=s",
variable => "vmname",
help => "Name of the Virtual Machine",
},
ipaddress => {
type => "=s",
variable => "ipaddress",
help => "IP address of the virtual machine",
},
powerstatus => {
type => "=s",
variable => " powerstatus",
help => "State of the virtual machine: poweredOn or poweredOff",
},
guest_os => {
type => "=s",
variable => "guest_os",
help => "Guest OS running on virtual machine",
},
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate(\&validate);
my %filter_hash = create_hash(Opts::get_option('ipaddress'),
Opts::get_option('powerstatus'),
Opts::get_option('guest_os'));
Util::connect();
my $vm_views = VMUtils::get_vms ('VirtualMachine',
Opts::get_option ('vmname'),
Opts::get_option ('datacenter'),
Opts::get_option ('folder'),
Opts::get_option ('pool'),
Opts::get_option ('host'),
%filter_hash);
#create_snapshots($vm_views);
#my $vm_views = shift;
my $sname = Opts::get_option ('sname');
foreach (@$vm_views) {
my $mor_host = $_->runtime->host;
my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
my $scheduler = OnceTaskScheduler->new(runAt => '2008-03-20T13:15:00');
eval {
my $for_schedule=$_->CreateSnapshot(name => $sname,
description => 'Snapshot created for virtual machine: '
. $_->name ,
memory => 0,
quiesce => 0);
my $scheduled_task_spec =
ScheduledTaskSpec->new(action => $for_schedule,
description => 'Sample scheduled task for snapshot',
enabled => 0,
name => 'Sample snapshot scheduled task',
scheduler => $scheduler);
creating scheduled task
my $scheduled_task_mgr->CreateScheduledTask(entity => $_, spec => $scheduled_task_spec);
};
}
Util::disconnect();
Snapshots of all the vm's extracted from get_vms methode are created
by calling the CreateSnapshot method and passing name of the snapshot and description
as parameters.
=====================================================================================
sub create_hash {
my ($ipaddress, $powerstatus, $guest_os) = @_;
my %filter_hash;
if ($ipaddress) {
$filter_hash{'guest.ipAddress'} = $ipaddress;
}
if ($powerstatus) {
$filter_hash{'runtime.powerState'} = $powerstatus;
}
if ($guest_os) {
$filter_hash{'config.guestFullName'} ='.' . $guest_os . '.';
}
return %filter_hash;
}
sub validate {
my $valid = 1;
my $powerstatus = Opts::get_option('powerstatus');
if ($powerstatus && !($powerstatus eq 'poweredOn') && !($powerstatus eq 'poweredOff')) {
Util::trace(0, "\nMust specify poweredOn"
."or poweredOff for 'powerstatus' parameter\n");
$valid = 0;
}
return $valid;
}
__END__
Hi,
Can you please attach the script instead of just pasting it? It will be much easier to debug and i can try it here on my own testenvironment.
One thing i noticed is, that you used 'http' instead of 'https' for the url. Is that intented?
Regards
Stefan
The error is because you're calling it like this:
new4.pl --url http://64.104.136.204/sdk/WebService --username usrnam --password psswd --vmname vm-test --sname usingperl
instead of https://64.104.136.204/sdk/WebService
The script creates a snapshot directy, it is unfortunatly not using the scheduler.
Stefan