rfrilling
Contributor
Contributor

Exclude VMs using the Remove Old Snapshots workflow in vRO

Jump to solution

Hello all! I'm hoping that someone has tackled this issue already. I have a 2 part requirement to remove all snapshots in our environments after the snapshot age has reached 72 hours but also to skip specific VM snapshots using a keyword like "Exclude". The canned workflow in vRO named Remove old snapshots accomplishes the first part of this requirement. I'm looking for guidance on how to modify this workflow to accomplish the second part of the requirement. I'm new to vRO so any help would be appreciated. Thanks!

Tags (1)
1 Solution

Accepted Solutions
Hejahida82
VMware Employee
VMware Employee

Hi rfrilling

Start by duplicating the workflow so that you can edit it.

In the duplicate workflow edit the Get snapshots scriptable task. At the end of the scriptable task you will see the following code:

if(days>numbrOfDay){

snapshots.push(ss);

System.log("The snapshot "+searchResults[i].folderPath +files[j].path+" of the VM "+ss.config.name+" had "+Math.floor(days)+" days");

snapshotProperties.remove(folderPath + files[j].path);

}

This if statement is controlling whether a snapshot is selected based on its age. To add an extra condition to check based on a keyword you can use the Snapshot name or description value and use the search method to see if your keyword is present and exclude any snapshots where the keyword is present.

The search method will return a value of -1 if the keyword is not found, so in your requirements you can use the -1 value returned by search to identify that the snapshot should be removed.

Using the snapshot name as an example your updated code would look like this, I've included the block of code above the snapshot date check as well as I'm capturing the snapshot name after the if statement which checks the snapshot value is not null:

if(ss){

//If a snapshot is found capture the snapshot name

var snapshotName = ss.name;

if(instanceObject==null){

instanceObject = VcPlugin.convertToVimManagedObject(ss , instance);

}

dateNow = instanceObject.currentTime();

timeForDateNow = dateNow.getTime();

timeForDateModif = files[j].modification.getTime();

diff = timeForDateNow-timeForDateModif;

days = diff/86400000;

//Now add a new condition to the if statement to check the date AND if the keyword is not present using a case insensitive search

//If the snapshot is old enough and does not contain the keyword in its name then add it to the array of snapshots to be removed

if((days>numbrOfDay) && (snapshotName.search(/keyword/i) == -1)){

snapshots.push(ss);

System.log("The snapshot "+searchResults[i].folderPath +files[j].path+" of the VM "+ss.config.name+" had "+Math.floor(days)+" days");

snapshotProperties.remove(folderPath + files[j].path);

}

The above code would replace the existing lines in the scriptable task, so the final Get snapshots scriptable task would look like this:

snapshots = new Array();

var searchResults = System.getModule("com.vmware.library.vc.vm.snapshot").getAllSnapshotResultInDatastoreBrowser(false,false,false,true) ;

var instance = new VcManagedObjectReference();

instance.type = "ServiceInstance";

instance.value = "ServiceInstance";

var instanceObject = null;

var dateNow;

var timeForDateNow;

var timeForDateModif;

var diff;

var days;

var ss;

var folderPath;

for (var i in searchResults) {

var files = searchResults[i].file;

for (var j in files) {

        folderPath = searchResults[i].folderPath;

        folderPath = folderPath.charAt(folderPath.length - 1) === '/' ? folderPath : folderPath + '/';

ss = snapshotProperties.get(folderPath + files[j].path);

if(ss){

var snapshotName = ss.name;

if(instanceObject==null){

instanceObject = VcPlugin.convertToVimManagedObject(ss , instance);

}

dateNow = instanceObject.currentTime();

timeForDateNow = dateNow.getTime();

timeForDateModif = files[j].modification.getTime();

diff = timeForDateNow-timeForDateModif;

days = diff/86400000;

if((days>numbrOfDay) && (snapshotName.search(/keyword/i) == -1)){

snapshots.push(ss);

System.log("The snapshot "+searchResults[i].folderPath +files[j].path+" of the VM "+ss.config.name+" had "+Math.floor(days)+" days");

snapshotProperties.remove(folderPath + files[j].path);

}

}

}

}

Hope this helps, let me know if you have any questions.

View solution in original post

4 Replies
Hejahida82
VMware Employee
VMware Employee

Hi rfrilling

Start by duplicating the workflow so that you can edit it.

In the duplicate workflow edit the Get snapshots scriptable task. At the end of the scriptable task you will see the following code:

if(days>numbrOfDay){

snapshots.push(ss);

System.log("The snapshot "+searchResults[i].folderPath +files[j].path+" of the VM "+ss.config.name+" had "+Math.floor(days)+" days");

snapshotProperties.remove(folderPath + files[j].path);

}

This if statement is controlling whether a snapshot is selected based on its age. To add an extra condition to check based on a keyword you can use the Snapshot name or description value and use the search method to see if your keyword is present and exclude any snapshots where the keyword is present.

The search method will return a value of -1 if the keyword is not found, so in your requirements you can use the -1 value returned by search to identify that the snapshot should be removed.

Using the snapshot name as an example your updated code would look like this, I've included the block of code above the snapshot date check as well as I'm capturing the snapshot name after the if statement which checks the snapshot value is not null:

if(ss){

//If a snapshot is found capture the snapshot name

var snapshotName = ss.name;

if(instanceObject==null){

instanceObject = VcPlugin.convertToVimManagedObject(ss , instance);

}

dateNow = instanceObject.currentTime();

timeForDateNow = dateNow.getTime();

timeForDateModif = files[j].modification.getTime();

diff = timeForDateNow-timeForDateModif;

days = diff/86400000;

//Now add a new condition to the if statement to check the date AND if the keyword is not present using a case insensitive search

//If the snapshot is old enough and does not contain the keyword in its name then add it to the array of snapshots to be removed

if((days>numbrOfDay) && (snapshotName.search(/keyword/i) == -1)){

snapshots.push(ss);

System.log("The snapshot "+searchResults[i].folderPath +files[j].path+" of the VM "+ss.config.name+" had "+Math.floor(days)+" days");

snapshotProperties.remove(folderPath + files[j].path);

}

The above code would replace the existing lines in the scriptable task, so the final Get snapshots scriptable task would look like this:

snapshots = new Array();

var searchResults = System.getModule("com.vmware.library.vc.vm.snapshot").getAllSnapshotResultInDatastoreBrowser(false,false,false,true) ;

var instance = new VcManagedObjectReference();

instance.type = "ServiceInstance";

instance.value = "ServiceInstance";

var instanceObject = null;

var dateNow;

var timeForDateNow;

var timeForDateModif;

var diff;

var days;

var ss;

var folderPath;

for (var i in searchResults) {

var files = searchResults[i].file;

for (var j in files) {

        folderPath = searchResults[i].folderPath;

        folderPath = folderPath.charAt(folderPath.length - 1) === '/' ? folderPath : folderPath + '/';

ss = snapshotProperties.get(folderPath + files[j].path);

if(ss){

var snapshotName = ss.name;

if(instanceObject==null){

instanceObject = VcPlugin.convertToVimManagedObject(ss , instance);

}

dateNow = instanceObject.currentTime();

timeForDateNow = dateNow.getTime();

timeForDateModif = files[j].modification.getTime();

diff = timeForDateNow-timeForDateModif;

days = diff/86400000;

if((days>numbrOfDay) && (snapshotName.search(/keyword/i) == -1)){

snapshots.push(ss);

System.log("The snapshot "+searchResults[i].folderPath +files[j].path+" of the VM "+ss.config.name+" had "+Math.floor(days)+" days");

snapshotProperties.remove(folderPath + files[j].path);

}

}

}

}

Hope this helps, let me know if you have any questions.

View solution in original post

rfrilling
Contributor
Contributor

Hejahida82, this is amazing! Thank you for putting the time into this solution. I'll be testing today and tomorrow and I'll let you know how it goes.

Thanks again!

0 Kudos
rfrilling
Contributor
Contributor

Hejahida82, all testing was successful. Thank you again for your time!

0 Kudos
Hejahida82
VMware Employee
VMware Employee

You are welcome, glad I could help.

0 Kudos