Solved

How to find orphaned virtual machine disks in VMM 2012/Hyper-V?

Posted on 2014-10-02
3
1,132 Views
Last Modified: 2014-11-12
I have noticed that virtual disks may be lying around for some reason, not attached to a virtual machine. Is there a way for me to find all of these disks so that I may delete them?

Earlier I have used VMTurbo to do this for VMWare, but I am not sure of the best approach in Hyper-V.
0
Comment
Question by:itnifl
  • 2
3 Comments
 
LVL 37

Expert Comment

by:Bing CISM / CISSP
ID: 40358724
that's a good question looking for a utility to find out all orphan disks not used by any VM of current host.

personally rather than depending on a utility to analyse the disks and determine the status, i simply always use meaningful filenames for the virtual disks beginning with their VM name then a short description for the disk purpose. therefore a simple DIR on command prompt or file search using Windows Explorer can do the job.
0
 
LVL 2

Accepted Solution

by:
itnifl earned 0 total points
ID: 40358764
In our environment there are hundreds of virtual machines, so a manual method is too cumbersome.
At first I found this blog and script, but it didn't work in our environment because of an error when using  the Get-VMHardDiskDrive cmdlet:
http://davestechnology.blogspot.no/2014/05/deleting-orphaned-hyper-v-disks-from.html

The error:
Get-VMHardDiskDrive : The cmdlet cannot find a specified class. Verify that the relevant feature is enabled on the operating system.

What feature that would be is unknown to me.

So I rewrote the  script to fit our environment and without the buggy cmdlet - List-OrphanedFiles.ps1:
[CmdletBinding(ConfirmImpact='High',SupportsShouldProcess=$true)]
PARAM (
	[string]$ClusterName = "ClusterNameHere",
	[string]$LocalClusterStoragePath = "C:\\ClusterStorage",
	[string]$ClusterStorageUNCTail = "C$\ClusterStorage",
	[string[]]$Extensions = @( '*.vhdx', '*.vhd', '*.iso' )
)

BEGIN {
	#Get all disks registered in Hyper-V:
	if ($host.Name -eq "ConsoleHost") {Write-Host -Foregroundcolor Yellow "Using the following parameters: "}
	$baseHost = (get-VMHostGroup | % {$_.AllChildClusters | where-object {$_.ClusterName -eq $ClusterName} | get-vmhost})[0];
	if ($host.Name -eq "ConsoleHost") {Write-Host -Foregroundcolor Yellow "   baseHost: $baseHost"}
	$baseHostname = $baseHost.Name.Split(".")[0];
	if ($host.Name -eq "ConsoleHost") {Write-Host -Foregroundcolor Yellow "   baseHostname: $baseHostname"}
	$baseCluster = (get-VMHostGroup | % {$_.AllChildClusters | where-object {$_.ClusterName -eq $ClusterName}});
	if ($host.Name -eq "ConsoleHost") {Write-Host -Foregroundcolor Yellow "   baseCluster: $baseCluster"}
	$uncBase = [string]$("\\"+$baseHostname+"\"+$ClusterStorageUNCTail); 
	if ($host.Name -eq "ConsoleHost") {Write-Host -Foregroundcolor Yellow "   uncBase: $uncBase"}
	$disks = $baseCluster | get-vmhost | Get-Vm | Foreach-Object {$_.virtualdiskdrives | % {[String]($_.VirtualHardDisk.Location) -replace $LocalClusterStoragePath, $uncBase}}
	
	$List = @($Extensions | ForEach-Object {
		$extension = $_;
		if ($host.Name -eq "ConsoleHost") {Write-Host -Foregroundcolor Yellow "- Filtering extension: $extension"}
		dir -Path $uncBase | % {
			dir -Path $uncBase\$_ | %{
				dir -Path $($uncBase+"\"+($_).Parent.Name+"\"+$_) -Filter $extension | Select-Object -ExpandProperty FullName					
			}
		}
	}) | Where-Object { 
		$_ -notin $disks 
	} | Select-Object -Unique
	if ($host.Name -eq "ConsoleHost") {Write-Host -Foregroundcolor Yellow $("`n**Found " + $List.count + " files..")}
	$List
}

Open in new window


Usage (if default parameters are set correct):
./List-OrphanedFiles.ps1 > files.txt

This script also lists iso files that have been copied in with the virtual machines. I guess from mounting them unshared instead if shared.

I guess I will have to set the script up at my script page or blog sometime. Until then I think a lucky Googler would find it useful finding it here on this site.
0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 40374591
Seems like there have been no other suggestion for a solution.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now