Solved

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

Posted on 2014-10-02
3
1,362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 37

Expert Comment

by:bbao
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is comprised of the pearls we have garnered deploying virtualization solutions since Virtual Server 2005 and subsequent 2008 RTM+ Hyper-V in standalone and clustered environments.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Suggested Courses

617 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