Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Know the reasons and solutions to move/import EDB to New Exchange Server. Also, find out how to recover an Exchange .edb file and to restore the file back.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

670 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