VMware Cloud Community
rubenra
Contributor
Contributor
Jump to solution

Search for template snapshots

Hi all,

I am making a script that searchs the unused files in the datastores and I want to serach for the template snapshots. I know that you can't create a snapshot of a template but if you convert a vm with snapshots to a template you get that. So my problem is that with the get-snapshot function I need a VM which I don't have because is a template. I can manage to get the snapshot name but I can't get the FileName of the hard disk of the snapshot which is what I want.

This is the command I am using to get the snapshot info:

Get-View -ViewType VirtualMachine -Filter @{"snapshot"="$snaplist"} | %{$_.snapshot.rootsnapshotlist}

Many thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($template in (Get-View -ViewType VirtualMachine -Filter @{"Config.Template"="True"})){ 
  if($template.Layout.Snapshot){
    $template.Layout.Snapshot |
    Select @{N="Template";E={$template.Name}},
      @{N="Snapshot Files";E={[string]::Join(',',$_.SnapshotFile)}}   } }


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($template in (Get-View -ViewType VirtualMachine -Filter @{"Config.Template"="True"})){ 
  if($template.Layout.Snapshot){
    $template.Layout.Snapshot |
    Select @{N="Template";E={$template.Name}},
      @{N="Snapshot Files";E={[string]::Join(',',$_.SnapshotFile)}}   } }


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

For more complete info you can do something like this

foreach($template in (Get-View -ViewType VirtualMachine -Filter @{"Config.Template"="True"})){ 
  if($template.LayoutEx.Snapshot){
    $diskTab = $template.Config.Hardware.Device |
      where {$_ -is [VMware.Vim.VirtualDisk]} | %{         @{$_.Key=$_.DeviceInfo.Label}       }     $fileTab = $template.LayoutEx.File | %{       @{$_.Key=$_.Name}     }     $snapshotTab = $template.Snapshot.RootSnapshotList | %{       @{$_.Snapshot.Value=$_.Name}     }     foreach($snapshot in $template.LayoutEx.Snapshot){       $snapshot.Disk |
      Select @{N="Template";E={$template.Name}},
      @{N="Snapshot";E={$snapshotTab[$snapshot.Key.Value]}},
      @{N="HD";E={$diskTab[$_.Key]}},
      @{N="Snapshot Files";E={         [string]::Join(',',($_.Chain | %{$_.FileKey | %{$fileTab[$_].Values}}))       }}     }   } }


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

0 Kudos
rubenra
Contributor
Contributor
Jump to solution

Thank you very much

0 Kudos