Locate inactive virtual hard disk files on Hyper-V Cluster Shared Volumes

Locate inactive virtual hard disk files on Hyper-V Cluster Shared Volumes

In this article we will look at how we can ease the process of doing housekeeping on Cluster Shared Volumes used for storing virtual machines in Hyper-V.

Hyper-V Virtual Disk Files (VHD or VHDX) might be inactive for a number of reasons:

  • The Virtual Machine was deleted from Hyper-V Manager, this will not delete the Virtual Disk Files
  • The Virtual Machine was deleted from System Center Virtual Machine Manager without the option to delete the virtual hard disk files
  • Old files and folders present from previous migrations

In addition, I am also working on  troubleshooting a bug together with Microsoft support where the source Virtual Disk Files does not get deleted when performing a Storage Live Migration in Windows Server 2012 R2, thus this might also be a possible reason for inactive Virtual Disk Files.

In this specific case I was tasked with providing a report of all inactive disk files in an environment with several hundred virtual machines stored on  a relatively large number of Cluster Shared Volumes. This would be a very cumbersome task to do manually, so I wrote a small PowerShell script which I`ve published here.

The script can be run from any computer with PowerShell 3.0 or later and the Virtual Machine Manager module installed. The following steps is performed by the script:

  • Retrieve all active Virtual Disk Files in use in the specified cluster (also including checkpoints/snapshots)
  • Retrieve all Cluster Shared Volumes from the specified cluster
  • Retrive all Virtual Disk Files present on the Cluster Shared Volumes
  • Compare the list of active files with the list of present files
  • Return the size of all inactive files

Note that the script is provided “as-is” in order to show how PowerShell can be leveraged to quickly build useful reports which can save us time in day to day operations. If the script was to be run in an automated fashion, additional error handling, logging and such should be added. Feel free to further extend the script with support for other storage scenarios like local disks, Scale-Out File Server shares and so on.

I could also have included an option to delete inactive files automatically, but as a precaution I would suggest that the returned list of inactive files is reviewed manually. The reason is that some files should be archived instead of deleted or if there is a bug in the script. I also had one occurance of a virtual machine present in the cluster which was not yet discovered by SC VMM, this would lead to the Virtual Disk Files for the virtual machine being reported as inactive.

At the environment I ran this script to generate a report of inactive Virtual Disk Files, we were able to reclaim close to 2 TB of data.

Bonus tip – ISO files may also use a lot of disk space if the “Share file instead of copying it” option is not leveraged. Here is a few one-liners to work with mounted ISO-files:

 

Jan Egil Ring works as a Lead Architect on the Infrastructure Team at Crayon, Norway. He mainly works with Microsoft server-products and has a strong passion for Windows PowerShell. In addition to being a consultant, he is a Microsoft Certified Trainer. He has obtained several certifications such as MCSE: Server Infrastructure and MCSE: Private Cloud. He is also a multiple-year recipient of the Microsoft Most Valuable Professional Award for his contributions in the Windows PowerShell technical community.

1 COMMENT

  1. Thanks for this script, I saved 2.3TB in one lab and 1TB in another! Exactly what I was looking for after spending a couple of hours manually checking the VM files.

Leave a Reply