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.

17 comments:



  1. What an awesome post, I just read it from start to end. Learned something new after a long time.



    Manual testing training in Chennai


    Selenium testing training in Chennai

    ReplyDelete
  2. Well written article and the topic that you have selected is too good. You are a blessed person and the way you executed the theme is highly appreciable
    Selenium Training in Chennai

    ReplyDelete
  3. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.


    aws training in bangalore | best aws training in bangalore

    ReplyDelete
  4. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject

    Virtual Machines & Scale-Sets |App Services|
    Cloud Services
    | Azure Active Director

    ReplyDelete
  5. I Got Job in my dream company with decent 12 Lacks Per Annum Salary, I have learned this world most demanding course out there in the current IT Market from the Data Science Training in Pune Providers who helped me a lot to achieve my dreams comes true. Really worth trying.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    Python Certification Course
    Digital Marketing Certification Course
    AWS Certification Course
    Selenium Online Course
    Data Science Certification Course
    DevOps Certification Course

    ReplyDelete