Consolidating Disks and Orphaned Snapshots with PowerCLI

Recently I have had some issues with HardDisks needing to be consolidated on VMs. I believe this is mostly due to VDR sometimes not handling snapshots the way it should, but I am sure there are other common ways it happens as well.

So what do I mean by disks needing to be consolidated…..

Holy Moly!!!! That is quite a bit of snapshots….Wait a second though, when I check the VM for snapshots, it says it doesn’t have any though 🙁   So then all these disks are there for no reason? Awesome! Love IT! All these extra disks could even cause some performance issues

I was first alerted to this issue when vCenter prompted me there was something going on…

However, since this first issue or two, I have noticed that vCenter doesn’t always let you know something is going on, so expecting a Warning is hit an miss.

But who wants to go through and check every VM to see if vCenter happens to let you know(and it might not) that there is something going on. I know I don’t.

So of course, as one might expect, I decided to write a script to let me know if there is an issue. But before the script I wanted to walk through the  equation I use in the script to determine whether there are disks that need to be consolidated.

(#ofHarddisks * 2) * (#ofSnapshots +1) < (#ofVMDKfilesforVM in Datastore)

So if the above statement is true, then a VM has disks that should be consolidated. But I just want to go through the equation just a bit more…

(#ofHarddisks * 2)= The number of HardDisks you would see if you go into vCenter and do the Edit Settings for VM. The multiplied by 2, is because for every HardDisk there are two associated .vmdk files.

* (#ofSnapshots +1) = Number of Snapshots a VM has +1. I understand this may be slightly confusing so lets do an example or two.

VM1 has 2 HardDisks and no snapshots.

(#ofHarddisks * 2)=4

* (#ofSnapshots +1)=1

Because there are no snapshots we would expect the number of .vmdk files to be the number of HardDisks * 2.

4*1=4, which it turns out to be!!!!

VM2 has 2 HardDisks and 2 Snapshots

(#ofHarddisks * 2)=4

* (#ofSnapshots +1)=3

There are 2 snapshots, so for each vmdk we should expect 2 copies of the original. In other words for each .vmdk we will have the original, the first snapshot, and the second snapshot….which is 3! So

4 * 3= 12, which is the number of vmdk files we should expect.

So getting back to the equation

(#ofHarddisks * 2) * (#ofSnapshots +1) < (#ofVMDKfilesforVM in Datastore)

So on the left side of the < sign is the expected number of vmdks for a VM, and on the right side is the actual. So when the right-hand side is bigger… there are disks that need to be consolidated.

I hope all this makes sense, as I am trying to be as transparent as possible. If it doesn’t make sense though, just know that one of my two Bachelor degrees is in Mathematics(the other Physics), and so I have your back 🙂

So here is that same equation, but transformed into a powercli one-liner which will return the VMs that have disks that need to be consolidated…

Get-VM|?{((get-harddisk $_).count*2)*((get-snapshot -vm $_).count + 1)`
 -lt ($_.extensiondata.layoutex.file|?{$_.name -like "*vmdk"}).count}

Fun Right?!?!! I thought so!

Alright so now we have identified which VMs are the offenders. So how do we fix it?

Right-Click on your VM->SnapShot-> Consolidate

Easy as Pie. But if you don’t want to do all the mousework…

$vms=Get-VM|?{((get-harddisk $_).count*2)*((get-snapshot -vm $_).count + 1)`
 -lt ($_.extensiondata.layoutex.file|?{$_.name -like "*vmdk"}).count}

$vms.extensiondata|%{$_.consolidatevmdisks_task()}

Thus far I have not had any problems with this method consolidating the disks on any of my VMs. When you do it through the GUI it does throw a warning about logs, but again, I haven’t had an issue, and hopefully I don’t ever have one 🙂

Good Luck, and I hope this helps!

3 Responses to Consolidating Disks and Orphaned Snapshots with PowerCLI

  1. Thanks for the math…

    Looks great and works well except that the count will return nothing if the result has only one object.

    Powershell count property considers one object as scalar and will return nothing. If the result is more than one you will get the correct value. So the way to workaround that would be to use @(..) array construction and place the result into an array.

    (@(get-harddisk $_).count*2)*(@(get-snapshot -vm $_).count + 1) -lt @($_.extensiondata.layoutex.file|?{$_.name -like “*vmdk”}).count

    Also we could extend it by piping the values into a report like this.

    Get-VM|?{(@(get-harddisk $_).count*2)*(@(get-snapshot $_).count + 1) -lt @($_.extensiondata.layoutex.file|?{$_.name -like “*vmdk”}).count} | Select Name, PowerState, @{N=”Disks”;E={(@(get-harddisk $_).count*2)}}, @{N=”Snaps”;E={(@(get-snapshot -vm $_).count)}}, @{N=”Vmdks”;E={(@($_.extensiondata.layoutex.file|?{$_.name -like “*vmdk”}).count)}} | Export-Csv -NoTypeInformation C:\VMDK_Orphan_Report.csv

Leave a Reply