I use Hyper-V Server 2008 R2 for running many virtual machines for different purposes. For all of these virtual machines, I either create new VHDs and re-use the existing ones. Here is the problem. I don’t have SCVMM or any similar management software to maintain the VHD library. But I need to keep track of various VHDs because any accidental deletion might lead to unusable virtual machine infrastructure. VHD Shell Extensions script I created a few months back came to my rescue here. If you have not downloaded or started using this script yet, you can do some by visting this page on MSDN Code gallery.
So, I just created a batch file wrapper around the VHDShellExt script. Here is what I have
@echo off
Set SCRIPTPATH=C:\Scripts
Set VHDPATH=E:\VHDs:: Find all VHDs from the given VHD Path and use VHDShellExt.vbs
for /f %%a IN (‘dir /b /s %VHDPATH%\*.vhd’) do “cscript %SCRIPTPATH% /action:detail /vhd:%%a”
%SCRIPTPATH% is the path to VHDShellExt.vbs and %VHDPATH% is the place where all your VHDs are stored. This script will validate all the VHDs against the local Hyper-V instance and attempts to find if any of the virtual machines are associated with the given VHDs. Since we are using VHDShellExt.vbs for finding stale VHDs, the output of the script will be similar to what you would usually see when you use VHD Shell extensions. So, for each VHD, there will be one line in the output that specifies if the VHD is associated with a VM or not. I will modify VHD Shell Extensions a bit to provide a different Hyper-V instance than just the loca one. That way, we will be able to use the same script even to check other Hyper-V instances on the network to find stale VHDs.




