VMware Cloud Community
rbadboy85
Contributor
Contributor

Remove old snapshots with vRealize Orchestrator

Hi All,

I used to have Powercli script in one of our windows server that ran every night at 1AM trough scheduled tasks and remove all snapshot with the age of 3 days automatically, for audit reason, I can't use that anymore.

The next option that I have is vRealize Orchestrator and I am struggling with it to get it work.

This is a first time that I am using Orchestrator so don't have a lot of knowledge about it.

I've added our vCenter to it, then I cloned Remove old snapshots, on the cloned workflow, I clicked on the scheduled workflow, recurrence everyday at 1AM, age in days set to 3 and no notification email, then I hit the submit, it created a task.

On the workflow Runs tab in the task menu, I have two logs from last two night and their status is waiting.

also, my snapshot age is 5 days, so it should work.

Thanks in advance

6 Replies
sbeaver
Leadership
Leadership

I have something you can use as a reference and you will need to re-engineer a little based on your environment.  Now since you have indicated that you have been using powershell for this task in the past I am going to step out on a limb and "assume" that you have heard of the vCheck scripts that are out there and were originally written as PowerShell scripts and have also been ported over to VCO and I would highly recommend finding that packed. ( I think you can find it at code.vmware.com)  The code below came from the 002_SnapshotInformation workflow in that set and I have taken this and some others a step further by adding code to fix the problem when it finds it and now just report on it.  This workflow has two main tasks, delete the snapshot and consolidate the disks

There are two inputs for this as is:

(ConfigurationElement)Configuration:

(Number)daysSnapshotAge:

var vcVirtualMachines = Configuration.getAttributeWithKey("vcVirtualMachines").value;

var maxSnapshotTime = new Date();

maxSnapshotTime.setDate(maxSnapshotTime.getDate()-daysSnapshotAge);

System.log( maxSnapshotTime );

try {

var findAllSnapshots = function( vcVirtualMachineSnapshotTreeArray ) {

if( vcVirtualMachineSnapshotTreeArray != null ) {

for each( var node in vcVirtualMachineSnapshotTreeArray ) {

System.log( node.createTime );

if( node.createTime < maxSnapshotTime ) {

var vmObject = {"name":node.vm.name, "snapshotName":node.name, "createTime":node.createTime, "action":"deleted" };

var task1 = node.snapshot.removeSnapshot_Task(false,true);

System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task1) ;

var task2 = node.vm.consolidateVMDisks_Task();

System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task2) ;

results.push( vmObject );

}

if( node.childSnapshotList != undefined ) {

var childTree = node.childSnapshotList;

if( childTree != null ) {

findAllSnapshots(childTree);

}

}

}

}

}

}catch(ex){

System.log(ex)

}

try{

for each( var vm in vcVirtualMachines ) {

if( vm.snapshot != undefined ) {

findAllSnapshots(vm.snapshot.rootSnapshotList);

}

  }

}catch(ex){

System.log(ex)

}

outputObject.results = results;

var output = JSON.stringify( outputObject );

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
sbeaver
Leadership
Leadership

I have something you can start with as a reference but you will need to edit it to match your needs.  This was based on the vCheck powershell scripts that were ported over to vCO (code.vmware.com

As is this takes 2 inputs

(ConfigurationElement)Configuration

(Number)daysSnapshotAge

var vcVirtualMachines = Configuration.getAttributeWithKey("vcVirtualMachines").value;

var maxSnapshotTime = new Date();

maxSnapshotTime.setDate(maxSnapshotTime.getDate()-daysSnapshotAge);

System.log( maxSnapshotTime );

try {

var findAllSnapshots = function( vcVirtualMachineSnapshotTreeArray ) {

if( vcVirtualMachineSnapshotTreeArray != null ) {

for each( var node in vcVirtualMachineSnapshotTreeArray ) {

System.log( node.createTime );

if( node.createTime < maxSnapshotTime ) {

var vmObject = {"name":node.vm.name, "snapshotName":node.name, "createTime":node.createTime, "action":"deleted" };

var task1 = node.snapshot.removeSnapshot_Task(false,true);

System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task1) ;

var task2 = node.vm.consolidateVMDisks_Task();

System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task2) ;

results.push( vmObject );

}

if( node.childSnapshotList != undefined ) {

var childTree = node.childSnapshotList;

if( childTree != null ) {

findAllSnapshots(childTree);

}

}

}

}

}

}catch(ex){

System.log(ex)

}

try{

for each( var vm in vcVirtualMachines ) {

if( vm.snapshot != undefined ) {

findAllSnapshots(vm.snapshot.rootSnapshotList);

}

  }

}catch(ex){

System.log(ex)

}

outputObject.results = results;

var output = JSON.stringify( outputObject );

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
rbadboy85
Contributor
Contributor

Hi sbeaver, thank you for responding.

I didn't work with that script, I'll try to figure it out.

Do you know why the pre-define workflow to remove old snapshot doesn't work on vRealize Orchestrator? I believe I am making mistake and some step, but can't find it.

Reply
0 Kudos
sbeaver
Leadership
Leadership

Depends on the error you are getting back.  You can try to make a copy of the workflow and then System.log every command so you can see what is going on also you should get some information from the vCenter logs about what it sees and if and which errors are showing up

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
iiliev
VMware Employee
VMware Employee

Hi,

The workflow should work. Could you tell me the versions and build numbers of your vCenter server, vRO server, and vCenter plug-in (if your vCenter plug-in is updated and not the same version that is bundled with vRO appliance)?

Reply
0 Kudos
rbadboy85
Contributor
Contributor

Hi,

VMware vRealize Orchestrator 7.4.0.8071781

vCenter: 6.0.0,5318200

it doesn't show the plugin on Webclient.

Reply
0 Kudos