Powershell script to get parent disk/path tree of VHD(x) file


In some situations you might find a VM disk to be part of a series of differencing disks. Take this example:

PPath1

If this was the result of VM Checkpoints, the fix would be easy: simply delete the checkpoints which merges the disks. But his is not the case. Hyper-V Manager shows no checkpoints:

PPath2

Powershell confirms (notice no output):

PPath3

This script is the first step to a fix. It lists the disk parent tree. The script can be downloaded from the Microsoft Script Center Repository.

To use this script, download it, run it as administrator, then use the function. To see help:

PPath5

You will see output like:

NAME
 Get-ParentPath
 
SYNOPSIS
 Function to get parent disk/path tree of VHD(x) file
 
SYNTAX
 Get-ParentPath [-VHDPath] <String> [[-ComputerName] <String>] [-WhatIf] [-Confirm] 
[<CommonParameters>]
 
DESCRIPTION
 Function to get parent disk/path tree of VHD(x) file
 
PARAMETERS
 -VHDPath <String>
 Full path to the VHD(x) file.
 For example: 
"I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C_55DB25B0-EFA9-415F-A5D1-738A62742B4E.avhdx"
 
 Required? true
 Position? 1
 Default value 
 Accept pipeline input? true (ByValue, ByPropertyName)
 Accept wildcard characters? false
 
 -ComputerName <String>
 Name of Hyper-V host where the VHD(x) file resides
 If absent, defaults to localhost
 
 Required? false
 Position? 2
 Default value .
 Accept pipeline input? false
 Accept wildcard characters? false
 
 -WhatIf [<SwitchParameter>]
 
 Required? false
 Position? named
 Default value 
 Accept pipeline input? false
 Accept wildcard characters? false
 
 -Confirm [<SwitchParameter>]
 
 Required? false
 Position? named
 Default value 
 Accept pipeline input? false
 Accept wildcard characters? false
 
 <CommonParameters>
 This cmdlet supports the common parameters: Verbose, Debug,
 ErrorAction, ErrorVariable, WarningAction, WarningVariable,
 OutBuffer, PipelineVariable, and OutVariable. For more information, see 
 about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 
 
INPUTS
 
OUTPUTS
 
NOTES
 Function by Sam Boutros
 v1.0 - 11/01/2014
 
 -------------------------- EXAMPLE 1 --------------------------
 
 Get-ParentPath -VHDPath 
"I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C_55DB25B0-EFA9-415F-A5D1-738A62742B4E.avhdx" 
-ComputerName "xhost16"
 
 Retunrs an array of the disk path and all its parents paths
 
 -------------------------- EXAMPLE 2 --------------------------
 
 $VMName = "v-2012R2-G2a"
 $HVName = "xHost16"
 $VMDisks=Invoke-Command -ComputerName $HVName -ArgumentList $VMName -ScriptBlock { 
     Param($VMName)
     Get-VMHardDiskDrive -VMName $VMName
 }
 ($VMDisks.Path | Where { $_ -match ":" }) | % {
     Get-ParentPath -VHDPath $_ -ComputerName $HVName
 }
 
 Retunrs an array for each disk attached to the VM $VMName, containing the disk tree.

 Sample output:
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C_55DB25B0-EFA9-415F-A5D1-738A62742B4E.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C_0020A6D3-0371-48E3-B67D-DE2ADF0BEDF1.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C_C2BA8DE5-8FE6-49AD-B12B-789853306524.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C_79E7AFEB-F867-4068-A3DA-6BF64F49E819.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C_232D8FD8-5855-4518-800C-2659385521FA.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-C.VHDX
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-D_C492420D-011D-42F4-862A-A04A7222B7FC.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-D_7B0A54C4-36E5-4229-9746-49566B0566AF.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-D_460EDAAF-F30A-4744-8396-87449EAF1A8C.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-D_9D3805B2-79F5-4E5F-865D-D69620E31B6A.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-D_F37513BE-AD00-41FD-B24E-CBB3E0BDEFC4.avhdx
 I:\VMs\v-2012R2-G2a\v-2012R2-G2a-D.vhdx
 
RELATED LINKS
 https://superwidgets.wordpress.com/category/powershell/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s