Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-02
3
Medium Priority
?
1,533 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

886 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