VMware {code} Community
pdhami1
Contributor
Contributor

create a snapshot and then destroy said snapshot in the same script.

I am putting together a script that will create a snapshot, doa few other tasks, then delete the snapshot.

I thought the following code would work but it doesnt seems to work consistantly, and if someone snapshots the same vm the script is working on the "current snapshot" is not the same.

my $ss = Vim::get_view (mo_ref=>$_->snapshot->currentSnapshot);

eval { $ss->RemoveSnapshot (removeChildren => 0 ); };

I would rather delete all snapshots with a particular name but I haven't had any luck doing such. RemoveAllSnapshots will not meet my needs.

Reply
0 Kudos
1 Reply
ssurana
VMware Employee
VMware Employee

Hi Ken,

The code below illustrates how you can delete multiple snapshots of a VM with the same name in a single go.

#!/usr/bin/perl -w

#Copyright (c) 2007 VMware, Inc. All rights reserved.

use strict;

use warnings;

use FindBin;

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

use VMware::VIM2Runtime;

use VMware::VILib;

$Util::script_version = "1.0";

Opts::parse();

Opts::validate();

Util::connect();

display_servertime();

Util::disconnect();

sub display_servertime ); # Get the VM of your choice by entering the VM name here

unless ($vm_view)

my $ss = $vm_view->snapshot->rootSnapshotList;

foreach (@$ss) {

deleteSnapshot($_);

}

}

sub deleteSnapshot {

my ($snaps) = @_;

print "\nAnalyzing Snapshot: ". $snaps->name;

if ($snaps->name eq 'delSnap') { # Replace the name of the snapshot you want to delete

print "\nDeleting the snapshot delSnap....";

eval {

Vim::get_view(mo_ref => $snaps->snapshot)->RemoveSnapshot (removeChildren => 0 );

};

if ($@)

}

if (defined $snaps->childSnapshotList) {

my $child = $snaps->childSnapshotList;

foreach (@$child) {

deleteSnapshot($_);

}

}

}

I hope this was what you were looking for.

~ Sidharth

Reply
0 Kudos