VMware Cloud Community
ghman
Enthusiast
Enthusiast
Jump to solution

VMSnapshot Age VCO Workflow

I am trying to delete snapshots of a certain age within a certain folder via a VCO workflow. I have looked at the workflow that comes standard with the library and a couple other examples. The issue with those is that they loop through every snapshot of every VM in every folder. In my environment that is literally thousands of snapshots (snapshots are being used incorrectly in this environment - but that is another story for another forum) and it takes a significant amount of time to get through the loops. My workflow currently runs through all VMs in a specifc VC:VMFolder and retrieves all VC:VirtualMachineSnapshot object for each VM. I need a way to find the age of the specific VC:VirtualMachineSnapshot. Is there an easier/faster way of doing this rather than brute force through every snapshot in the system?


var created = thisSnapshot.creationTime();?? would be great.

Thanks all.

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Using the scripting code of action com.vmware.library.vc.vm.snapshot.getAllSnapshotsOfVM() as a base, here is how to enumerate all snapshots of your VM and print their IDs and the time when they were taken:

// 'vm' is the input parameter - the virtual machine object

var snapshots = new Array();

if (vm.snapshot) {

    var snapshotTrees = vm.snapshot.rootSnapshotList;

    for (i in snapshotTrees) {

        getSnapshotsOfVM(snapshotTrees[i]);

    }

}

function getSnapshotsOfVM(tree) {

    snapshots.push([tree.snapshot, tree.createTime]); // store a tupple of the snapshot and its creation time

    var trees = tree.childSnapshotList;

    if (trees != null) {

        for (index in trees) {

            if (trees[index] != null)

                getSnapshotsOfVM(trees[index]);

        }

    }

}

// Print the results

for each (var s in snapshots) {

    System.log("Snapshot: '" + s[0].id + "' created on: '" + s[1] + "'");

}

If you don't want to collect creation times for all snapshots of your virtual machine, but only creation time of a given snapshot, then just add one addition check on line 11 to compare the id attribute of your snapshot with the id attribute of the currently processed snapshot tree.snapshot.

View solution in original post

0 Kudos
6 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

The creation time of a snapshot can be retrieved not from VC:VirtualMachineSnapshot object but from VC:VirtualMachineSnapshotTree object. The property is called createTime (of type date).

I'm not sure if there is a direct link from VirtualMachineSnapshot to VirtualMachineSnapshotTree, but if you iterate over all snapshots of a VM, you can get access to snapshot trees using vm.snapshot.rootSnapshotList and retrieve creation times for all snapshots for this particular VM.

0 Kudos
ghman
Enthusiast
Enthusiast
Jump to solution

Ilian Iliev - thanks for the direction. I have found that:


VC:VirtualMachine has property snapshot of type VirtualMachineSnapshotInfo

and VirtualMachineSnapshotInfo has property rootSnapshotList of type VirtualMachineSnapshotTree[]

and VirtualMachineSnapshotTree[] has property createTime of type xsd:dateTime


I'm just having a hard time putting this into code in an Orchestrator workflow and getting it working.

I have a valid VC:VirtualMachine and a valid VC:VirtualMachineSnapshot form that VM and I can't seem to find the right function or property calls to arrive at something with the createTime property. Could you provide some help in this area?


Thanks again.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Using the scripting code of action com.vmware.library.vc.vm.snapshot.getAllSnapshotsOfVM() as a base, here is how to enumerate all snapshots of your VM and print their IDs and the time when they were taken:

// 'vm' is the input parameter - the virtual machine object

var snapshots = new Array();

if (vm.snapshot) {

    var snapshotTrees = vm.snapshot.rootSnapshotList;

    for (i in snapshotTrees) {

        getSnapshotsOfVM(snapshotTrees[i]);

    }

}

function getSnapshotsOfVM(tree) {

    snapshots.push([tree.snapshot, tree.createTime]); // store a tupple of the snapshot and its creation time

    var trees = tree.childSnapshotList;

    if (trees != null) {

        for (index in trees) {

            if (trees[index] != null)

                getSnapshotsOfVM(trees[index]);

        }

    }

}

// Print the results

for each (var s in snapshots) {

    System.log("Snapshot: '" + s[0].id + "' created on: '" + s[1] + "'");

}

If you don't want to collect creation times for all snapshots of your virtual machine, but only creation time of a given snapshot, then just add one addition check on line 11 to compare the id attribute of your snapshot with the id attribute of the currently processed snapshot tree.snapshot.

0 Kudos
ghman
Enthusiast
Enthusiast
Jump to solution

Much appreciated. I made a few changes to suite my workflow and it works great. thank you for the help.

0 Kudos
ghman
Enthusiast
Enthusiast
Jump to solution

One additional question - is there a way to convert the createTime to epoch milliseconds?

0 Kudos
ghman
Enthusiast
Enthusiast
Jump to solution

timeCreate.getTime()

I found this. Thanks again.

0 Kudos