The perl script which I used for scheduling a snapshot in virtual center is attached below. i Executed this perl script as
new4.pl --url http://64.104.136.204/sdk/WebService --username usrnm --password psswd --vmname vm-test --sname testing-snapshot-using-perl
It showed no error but i couldnt get any scheduled tasks in the scheduled tasks tab in the virtual center and also it didnt do the process as scheduled. Please help me in this matter.
is the date and time format right: year-month-dateThours:minutes:seconds
my code is:
#!/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-19T16:57:00');
eval {
my $for_schedule=$vm_views->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 => $vm_views, 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,
I still see some syntax-errors.
Everytime you want to use $vm_views inside a foreach (@$vm_views) { } you need to refer to it as $_. If you don't do this, perl thinks that you're declaring a new variable inside of the scope (regardless if it has been declared with the same name before).
E.g.
foreach (@$vm_views) { print $_->name; # instead of $vm_views->name which would be empty }
Hope this helps
Regards
Stefan