Search This Blog

Monday, April 8, 2013

Lightning Fast One-liner to get a list of all VM's with Snapshots

There are others, but I think this method rules. I'll first list the entire one-liner then break it down by color code:

Get-View -ViewType VIrtualMachine -Property Name , SnapShot, LayoutEx -Filter @{"Snapshot.CurrentSnapshot" = "snapshot"  } | ? { $_.name -match "$VM" } |
Select @{"N"="VM" ;  E={$_.Name}},
@{N= "vCenter"      ; E=  { $_.Client.ServiceUrl.Split("/")[2].split(":")[0]  }},
@{N= "Snapshots"    ; E = { (($_.LayoutEx.Snapshot | % { $_.Key }) | measure).count  }  },
@{N= "SnapsSizeGB"  ; E = { (($_.LayoutEx.File | ? { $_.Type -match "snapshotdata" } | measure -Property size -Sum ).sum *3 / 1gb ).ToString("#,0.00")   }  } ,
@{N= "OldestSnapshot"  ; E = { $_.Snapshot.RootSnapshotList | sort createtime | select -F 1  createtime | % { $_.CreateTime } } },
@{N= "DaysOld"  ; E = { $_.Snapshot.RootSnapshotList | sort createtime | select -F 1  createtime | % { ((Get-Date) - $_.CreateTime ).Days } } } | sort -desc DaysOLD

The first statement:

Get-View -ViewType VIrtualMachine -Property Name , SnapShot, LayoutEx -Filter @{"Snapshot.CurrentSnapshot" = "snapshot"  }

The above line will simply return all VM's with Snapshots. It's that simple, and it's very fast. Next the results are piped to a Where filter.

Notice the "$VM" variable? Use it to filter your script by VM names, and remember this that field is a regular expression, so you can't use the typical  wild card (* or ?) characters.  Normally, the -Filter array would contain "Name"="$VM", but doing that here would present an "OR" filter statement which would return wrong results. So we filter on found items in Get-View.

? { $_.name -match "$VM" } 

The final statement is a Select statement massages the results into these kind of results:

VM
vCenter
Snapshots
SnapsSizeGB
OldestSnapshot
DaysOld
Windows2003r2_Std_Tpl vc2wrvc06.int.loc 2
0.00
20/28/2022 2:56:22 PM 527
2622PPPHMR3 vc2wrvc03.int.loc 2 22.00 6/22/2022 3:59:26 PM 290
2622PPPH2 vc2wrvc03.int.loc 2 72.00 7/28/2022 7:33:36 PM 263
2650ppph2 vc2wrvc03.int.loc 2 72.00 7/28/2022 7:35:02 PM 263
2572ppph2 vc2wrvc02.int.loc 2 72.00 7/28/2022 7:32:25 PM 263
ar2550ppph2 vc2wrvc02.int.loc 2 72.00 7/28/2022 2:32:40 PM 263
2568xptest2 vc2wrvc04.int.loc 2 6.05 2/6/2023 20:38:56 PM 60
2568XPTEST3 vc2wrvc04.int.loc 2 24.20 2/23/2023 5:24:22 PM 53
2568XPTEST4 vc2wrvc04.int.loc 2 22.05 2/23/2023 5:24:06 PM 53
0223CTXWEB02 vc2wrvc03.int.loc 2 22.05 2/28/2023 20:55:03 PM 48
0223CTXWEB02 vc2wrvc03.int.loc 3 36.25 2/20/2023 9:53:43 PM 46
0223CTXSQL02 vc2wrvc03.int.loc 2 22.05 2/22/2023 9:56:39 PM 44
2568CTXWEB04 vc2wrvc04.int.loc 2 0.00 2/26/2023 7:56:02 PM 40
2706AMRSCN21 vc2wrvc04.int.loc 2 9.05 3/26/2023 7:00:54 PM 22
2568AMRDBE02 vc2wrvc04.int.loc 0 0.00 4/4/2023 22:08:25 AM 4

One problem I find is the snapshot size.  The Get-View VirtualMachine  object returns LayoutEx.File objects, but these objects do not report the same size disk as the Get-Snapshot returns. 

However, when I visually inspect the actual snapshot disk on the VMFS volume, I find that LayoutEx.File size is accurate and the Get-Snapshot is incorrect.  If I'm missing something, let me know.