VMware Cloud Community
g2490084
Contributor
Contributor

Need help writing a function that returns a complete list of snapshots (working with Get-View)

Hello,

I get VMs with Get-View, example:

$vm = Get-View -ViewType VirtualMachine -Filter @{ Name='abc123' }

Next, I would like to retrieve all snapshots and maybe generate a nicely formatted output (such as a list).
It occurs to me that the property $vm.Snapshot is not a simple list (array), but it can contain N snapshots (that you would see on the same level in Snapshot Manager with vSphere client) and each snapshot in its turn might have a chain of child snapshots.

There are various snippets of code floating around the internet, but all seem to ignore the crucial detail of child snapshots.
I may be wrong, I often am, but I think there is a need to write a recursive algorithm!
This is what I have so far, any help would be greatly apperciated!

    if ($vm.snapshot -ne $null)

    {

    $result = @()

    ForEach ($snap in $vm.Snapshot.RootSnapshotList)

    {

    $row = "" | Select SnapshotName, SnapshotCreated, Description | Sort -Property "SnapshotCreated"

    $row.SnapshotName = $snap.Name

    $row.SnapshotCreated = $snap.CreateTime

    if ($snap.Description.Length -lt 30) { $row.Description = $snap.Description } else { $row.Description = $snap.Description.Substring(0,30) + "[..]" }

    $Result += $row

   

    if ($snap.ChildSnapshotList -ne $null)

      {

        while ($snap.ChildSnapshotList -ne $null)

          {

            ?

          }

      }

    }

}

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

Not all!

In my post named UML diagram your VM, vdisks and snapshots, I show how to traverse all the snapshots.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
g2490084
Contributor
Contributor

LucD, what can I say, you really earned those guru/vExpert badges...:smileycool:
Thank you very much!

Reply
0 Kudos