4 Replies Latest reply on Oct 26, 2018 1:57 AM by guyrleech

    Correlating ESXi snapshot name to corresponding vmdk file

    guyrleech Virtuoso

      I'm working with raw ESXi 6.7 without vCenter using PowerCLI 11.0.0. What I need to be able to do is to figure out which *-0000*.vmdk file (the text descriptor file) corresponds to a particular snapshot returned from Get-Snapshot.

       

      There don't seem to be any properties of the snapshot objects that contain disk information that I can see.

       

      Getting the .vmdk files in that VM's folder via the vmstore: PS drive is easy enough but it obviously doesn't tell you which snapshot a particular *0000* vmdk file belongs to.

       

      Can I simply sort the snapshots on the created date and the earliest will be 000001, the next oldest 000002 and so on? I'm not sure how this approach would cope with deleted/consolidated snapshots though.

       

      I was also wondering if the order of the objects returned by Get-Snapshot corresponds to the number in the vmdk name so the 1st one returned is 000001 and so on.

       

      I've looked at the .vmsd file but that doesn't contain vmdk info.

       

      I guess I could also work my way through the snapshots looking at the ParentSnapshot and Children to make an ordered list but there's still no guarantee of disk numbering correlation I fear.

       

      ESXi must know so how does it do that - any ideas folks please?

       

      The reason I need this is that I'm writing a script to clone VMs from other VMs but without vCenter so there's no built-in templating and using something like -LinkedClone to New-VM gives an unsupported error as you'd kind of expect. I can clone from base disks ok, even renaming them after copying, but I'd like to add reliable linked clones too, to save on disk space if nothing else.

        • 1. Re: Correlating ESXi snapshot name to corresponding vmdk file
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          I'm afraid you can't rely on the number in the filename of the VMDK.

          When snapshot are removed and new ones created, there might be missing numbers in the sequence.

           

          The information about the snapshots and the files is kept in the VirtulaMachine object.

          Since the snapshots are organised in a tree, with many potential branches, it is easier to use a recursive function that traverses the complete tree.

          Something like this

           

          function Get-SnapVMDK{

              param(

                  [VMware.Vim.VirtualMachineSnapshotTree]$Snap

              )

           

              $vm = Get-View -Id $Snap.VM

              $s = $vm.Layout.Snapshot | where{$_.Key -eq $Snap.Snapshot}

              New-Object PSObject -Property @{

                  VM = $vm.Name

                  Snapshot = $Snap.Name

                  Created = $Snap.CreateTime

                  VMDK = $s.SnapShotFile[-1]

              }

              $Snap.ChildSnapshotList | where{$_} | %{

                  Get-SnapVMDK -Snap $_

              }

          }

           

          Get-VM | where{$_.ExtensionData.Snapshot.RootSnapshotList} |

          ForEach-Object -Process {

              $_.ExtensionData.Snapshot.RootSnapshotList |

              ForEach-Object -Process {

                  Get-SnapVMDK -Snap $_

              }

          }

           

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Correlating ESXi snapshot name to corresponding vmdk file
            guyrleech Virtuoso

            Thank you very much, that really helps although the vmdk file returned for the snapshot is the parent disk, not the delta one for this snapshot which is actually what I'm after so I guess I'll have to look at the immediate child snapshots' disks since their parent vmdk will be the delta disk I need (assuming there's only one immediate child snapshot).

            • 3. Re: Correlating ESXi snapshot name to corresponding vmdk file
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              In that case you perhaps might want to have a look at my UML Diagram Your VM, Vdisks And Snapshots post.

              Not necessarily to produce the UML diagram, but in there the logic to find all snapshot related files is there.

               

              As you can see in this example UML diagram.

              uml.png

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Correlating ESXi snapshot name to corresponding vmdk file
                guyrleech Virtuoso

                Thank you very much, that's just what I needed so I've hacked your function to return me the differencing disk for a given snapshot name.