Solved

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

Posted on 2014-10-02
3
1,233 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: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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

772 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