VMware {code} Community
saravana_k_r
Contributor
Contributor

snapshot schedule

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

#

  1. 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);

  1. creating scheduled task

my $scheduled_task_mgr->CreateScheduledTask(entity => $_, spec => $scheduled_task_spec);

};

}

Util::disconnect();

  1. Snapshots of all the vm's extracted from get_vms methode are created

  2. by calling the CreateSnapshot method and passing name of the snapshot and description

  3. as parameters.

  4. =====================================================================================

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__

0 Kudos
3 Replies
StefanPahrmann

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

0 Kudos
saravana_k_r
Contributor
Contributor

I have attached the perl script for creating the snapshot scheduling in this post. i changed the http to https while executing the perl script in command prompt. but it showed no error. but i couldnt see any new row in the scheduled tasks tab. Please help me in this issue.

0 Kudos
StefanPahrmann

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

0 Kudos