Free Hyper-V Script: Virtual Machine Storage Consistency Diagnosis

Hyper-V allows you to scatter a virtual machine’s files just about anywhere. That might be good; it might be bad. That all depends on your perspective and need. No good can come from losing track of your files’ sprawl, though. Additionally, the placement and migration tools don’t always work exactly as expected. Even if the tools work perfectly, sometimes administrators don’t. To help you sort out these difficulties, I’ve created a small PowerShell script that will report on the consistency of a virtual machine’s file placement.

How the Script Works

I designed the script to be quick and simple to use and understand. Under the hood, it does these things:

Gathers the location of the virtual machine’s configuration files. It considers that location to be the root. Gathers the location of the virtual machine’s checkpoint files. It compares that to the root. If the locations aren’t the same, it marks the VM’s storage as inconsistent. Gathers the location of the virtual machine’s second-level paging files. It compares that to the root. If the locations aren’t the same, it marks the VM’s storage as inconsistent. Gathers the location of the virtual machine’s individual virtual hard disk files. It compares each to the root. If the locations aren’t the same, it marks the VM’s storage as inconsistent. Gathers the location of any CD or DVD image files attached to the virtual machine. It compares each to the root. If the locations aren’t the same, it marks the VM’s storage as inconsistent. Emits a custom object that contains: The name of the virtual machine The name of the virtual machine’s Hyper-V host The virtual machine’s ID (a string-formatted GUID) A boolean ($true or $false) value that indicates if the virtual machine’s storage is consistent An array that contains a record of each location. Each entry in the array is itself a custom object with these two pieces: The component name(Configuration, Checkpoints, etc.) The location of the component

It doesn’t check for pass-through. A VM with pass-through disks would have inconsistent storage placement by definition, but this script completely ignores pass-through.

The script doesn’t go through a lot of validation to check if storage locations are truly unique. If you have a VM using “\\system\VMs” and “\\system\VMFiles” but they’re both the same physical folder, the VM will be marked as inconsistent.

Script Usage

I built the script to be named Get-VMStorageConsistency. All documentation and examples are built around that name. You must supply it with a virtual machine. All other parameters are optional.

Use Get-Help Get-VMStorageConsistency -Full to see the built-in help.

Parameters:

VM (aliases “VMName” and “Name”): The virtual machine(s) to check. The input type is an array, so it will accept one or more of any of the following: A string that contains the virtual machine’s names A VirtualMachine object (as output from Get-VM, etc.) A GUID object that contains the virtual machine’s ID A WMI Msvm_ComputerSystem object A WMI MSCluster_Resource object

(aliases “VMName” and “Name”): The virtual machine(s) to check. The input type is an array, so it will accept one or more of any of the following: ComputerName : The name of the host that owns the virtual machine. If not specified, defaults to the local system. Ignored if VM is anything other than a String or GUID. Only accepts strings.

: The name of the host that owns the virtual machine. If not specified, defaults to the local system. Ignored if is anything other than a String or GUID. Only accepts strings. DisksOnly : A switch parameter (include if you want to use it, leave off otherwise). If specified, the script only checks at the physical storage level for consistency. Examples: With this switch, C:\VMs and C:\VMCheckpoints are the same thing (simplifies to C:\) With this switch, C:\ClusterStorage\VMs1\VMFiles and C:\ClusterStorage\VMs1\VHDXs are the same thing (simplifies to C:\ClusterStorage\VMs1) With this switch, \\storage1\VMs\VMFiles and \\storage1\VMs\VHDFiles are the same thing (simplifies to \\storage1\VMs) Without this switch, all of the above are treated as unique locations

: A switch parameter (include if you want to use it, leave off otherwise). If specified, the script only checks at the physical storage level for consistency. Examples: IgnoreVHDFolder : A switch parameter (include if you want to use it, leave off otherwise). If specified, ignores the final “Virtual Hard Disks” path for virtual hard disks. Notes: With this switch, VHDXs in “C:\VMs\Virtual Hard Disks” will be treated as though they were found in C:\VMs With this switch, VHDXs in “C:\VMs\Virtual Hard Disks\VHDXs” will not be treated specially. This is because there is a folder underneath the one named “Virtual Hard Disks”. With this switch, a VM configured to hold its checkpoints in a folder named “C:\VMs\Virtual Hard Disks\” will not treat its checkpoints especially. This is because the switch only applies to virtual hard disk files.

: A switch parameter (include if you want to use it, leave off otherwise). If specified, ignores the “Virtual Hard Disks” path for virtual hard disks. Notes: Verbose: A built-in switch parameter. If specified, the script will use the Verbose output stream to show you the exact comparison that caused a virtual machine to be marked as inconsistent. Note : The script only traps the first item that causes a virtual machine to be inconsistent. That’s because the Consistent marker is a boolean; in boolean logic, it’s not possible to become more false. Therefore, I considered it to be wasteful to continue processing. However, all locations are stored in the Locations property of the report. You can use that for an accurate assessment of all the VMs’ component locations.

The Output Object

The following shows the output of the cmdlet run on my host with the -IgnoreVHDFolder and -Verbose switches set:

Output object structure:

Name : String that contains the virtual machine’s name

: String that contains the virtual machine’s name ComputerName : String that contains the name of the Hyper-V host that currently owns the virtual machine

: String that contains the name of the Hyper-V host that currently owns the virtual machine VMId : String representation of the virtual machine’s GUID

: String representation of the virtual machine’s GUID Consistent : Boolean value that indicates whether or not the virtual machine’s storage is consistently placed

: Boolean value that indicates whether or not the virtual machine’s storage is consistently placed Location: An array of custom objects that contain information about the location of each component

Location object structure:

Component : A string that identifies the component. I made these strings up. Possible values: Configuration : Location of the “Virtual Machines” folder that contains the VM’s definition files (.xml, .vmcx, .bin, etc.) Checkpoints : Location of the “Snapshots” folder configured for this virtual machine. Note : That folder might not physically exist if the VM uses a non-default location and has never been checkpointed. SecondLevelPaging : Location of the folder where the VM will place its .slp files if it ever uses second-level paging. Virtual Hard Disk : Full path of the virtual hard disk. CD/DVD Image : Full path of the attached ISO.

: A string that identifies the component. I made these strings up. Possible values:

An example that puts the object to use:

Get-VM | .\Get-VMStorageConsistency.ps1 -IgnoreVHDFolder | ? Consistent -ne $true | select Name 1 Get-VM | . \ Get-VMStorageConsistency . ps1 -IgnoreVHDFolder | ? Consistent -ne $true | select Name

The above will output only the names of local virtual machines with inconsistent storage.

Script Source

The script is intended to be named “Get-VMStorageConsistency”. As shown, you would call its file (ex.: C:\Scripts\Get-VMStorageConsistency). If you want to use it dot-sourced or in your profile, uncomment lines 55, 56, and 262 (subject to change from editing; look for the commented-out function { } delimiters).