Test Hyper-V VHD Folders with PowerShell

I’ve recently added a USB 3.0 Express Card adapter to my laptop to provide USB 3.0 functionality. The added performance is very useful in my Hyper-V setup. However, I am running into a glitch that I have yet to figure out where the external drives (the Express card has 2 ports) are still attached and recognized, but the file system is wonky. I can see most of the folders, and files but not really. This is a problem when I fire up a Hyper-V virtual machine with a VHD in one of these folders. Even though I can run a DIR command and see the file, it really isn’t there. So I threw together a little function to add to my Hyper-V workflow module to validate folders with VHD and VHDX files.

My virtual machines are spread out among a number of drives. I don’t necessarily need to validate every VHD file, but I would like to know that the paths exist. Now, I could simply pass my list of paths to Test-Path.

But my little glitch can lead to a false positive. So I need to take an extra step and validate one of the VHD/VHDX files using the Test-VHD cmdlet. Here’s my function.

The function gets the first VHD or VHDX file in each folder and tests it. If the test fails, Test-VHD throws an exception which I catch and then bail out. Otherwise, if there are no errors, the function writes $True to the pipeline. Otherwise, I’ll get an exception.

test-vhdpathBut now I have a tool to quickly verify paths before I start trying to launch virtual machines.

If you’d like to learn more about Test-VHD, take a look at my article on the Altaro Hyper-V blog. If you want to try out my function, you should be able to toggle to plain code and copy and paste.

 

Hyper-V VHD Summary

When I made the move to Windows 8, one of my tasks was to migrate my test environment from VirtualBox to Hyper-V. Windows 8 includes a client Hyper-V feature that is easy to use and includes PowerShell support. Plus I needed to expand my Hyper-V skills anyway, especially from the console. After setting up a number of VMs, I realized I needed to get a handle on disks: what files were in use for which VM? The Get-VM cmdlet writes a VirtualMachine object to the pipeline. One of its properties is HardDrives.

The property is actually a nested object.

That tells me where the VHD is, but not about it. For that I can use Get-VHD.

There is some great information here like sizes, type and format. All I need now is a way to combine all of this information so that for every VM I can get a summary of pertinent VHD information. This is a great scenario for using Select-Object and creating a custom property.

In the hash table, the Expression script block is running Get-VHD and returning the Size property. Eventually I want this for all VMs which means I’ll need to expand the HardDrives property.

That is very promising. In fact, let’s cut to the chase.

This function writes a summary object for each VM which means I can further sort, filter or whatever I need to do. Because this can take a bit of time to run, I’ll save the results to a variable.

One unexpected bonus was that it helped me identify a VM with a “bad” VHD reference.

This was a file I had renamed, which “broke” the virtual machine. Or I can see how much space my VHD files are taking.

Or perhaps I need to back them all up.

Awesome. But…..my function is based on code I developed to run from the command line which works great as a one line command. I’m not likely to have a great number of virtual machines so performance isn’t that big a deal. Plus I could always run it as a job. If I’m going to turn this into a function, perhaps it makes sense to break this up.

If you look at my function, I’m running Get-VHD multiple times for the same file. It probably makes more sense to only get it once. Here’s a revised block of code.

I’ll get the same result, and actually faster. My first version took 7.8 seconds and this takes 2.3 seconds. Because this is a function, I only have to type it once so I can add comments and even a progress message with Write-Host. This isn’t too say I can’t do this all from the console; it is just a bit more tedious.

The purpose of my post is to not only demonstrate some of the Hyper-V cmdlets, but also that what you type at the console doesn’t always make the best PowerShell script. Sometimes you need to re-think things for performance and maintainability.

Download Get-VHDSummary.