Bas Roovers
Bas Roovers Author of onpremisys and working in IT for over 10 years. Loves good discussions and new technologies.

Find orphaned VHDs in Hyper-V cluster

Find orphaned VHDs in Hyper-V cluster

If you’re running a Hyper-V cluster for a while, you probably ended up with a couple of virtual hard disks for which the actual VM is already gone. These orphaned VHDs are occupying valuable space and are kinda difficult to trace. I decided to dive into this and hoped that someone has already solved this puzzle.

I found the following scripts which advertise to do the job:

That didn’t really help so I decided to be bold and write my own version. Mine is meant to be run on clusters where all of your Hyper-V nodes use the same storage like with a SAN-based setup. It’s 36 lines and gets the job done within a minute for over 200 VMs.

You can run this Powershell script against any one of your nodes directly or by running it through Invoke-Command.

$clusterNodes = Get-ClusterNode | Select-Object Name -ExpandProperty Name

if(!$clusterNodes){Throw "No nodes found, please run this script on one of the nodes."}

Write-Host "The following nodes are part of the cluster: "
$clusterNodes | ForEach-Object { Write-Host "- $($_.Name)" }
Write-Host ""

Write-Host "Fetch VHDs from nodes: " -NoNewline
$ActiveVHDs = (Get-VM -ComputerName $clusterNodes | Get-VMHardDiskDrive | Select-Object -Property Path).Path
Write-Host "V" -ForegroundColor Green
Write-Host ""


Write-Host "Fetch VHDs from storage: " -NoNewline
$HyperVFileLocation = ("C:\ClusterStorage")
$Dir = Get-ChildItem $HyperVFileLocation -Recurse -ErrorAction Ignore | Where-Object {$_.FullName -notmatch "\\Replica\\?" } 
$AllVHDs = ($Dir | Where-Object { $_.Extension -eq ".vhd" }).FullName + ($Dir | Where-Object { $_.Extension -eq ".vhdx" }).FullName
Write-Host "V" -ForegroundColor Green
Write-Host ""

Write-Host "Compare VHDs of storage with nodes: " -NoNewline
$orphanedVHDs = @()
foreach($vhd in $AllVHDs){
    if($vhd -notin $ActiveVHDs){
        $orphanedVHDs += $vhd
    }
}
Write-Host "V" -ForegroundColor Green
Write-Host ""
if($orphanedVHDs){
    Write-Host "List of orphaned VHDs:"
    $orphanedVHDs
} else {
    Write-Host "Nice work, no orphaned VHDs found!" -ForegroundColor Green
}

comments powered by Disqus