VMware datastores can hide many misdemeanours

vCenter datastores can secretly hold vast quantities of unwanted data: virtual machines removed from inventory but not deleted from disk.  Deleted disks on virtual machines that just hang around on the data store eating up space.  I’ve found this can, over time, reduce your expensive storage capacity and leave you buying more storage unnecessarily, but i couldn’t find anything to identify these files, except in VSphere Orchestrator.

As a lover of the Powershell….


# http://www.powershellpro.com/powershell-tutorial-introduction/powershell-functions-filters/
filter Get-BestValue {
“{0:N2} {1}” -f $(
if ($_ -lt 1kb) { $_, ‘B’ }
elseif ($_ -lt 1mb) { ($_/1kb), ‘KB’ }
elseif ($_ -lt 1gb) { ($_/1mb), ‘MB’ }
elseif ($_ -lt 1tb) { ($_/1gb), ‘GB’ }
elseif ($_ -lt 1pb) { ($_/1tb), ‘TB’ }
else { ($_/1pb), ‘PB’ }
)
}

‹#
.SYNOPSIS
Retrieve the files in one or more datastores that are not in use by a virtual machine.

.DESCRIPTION
Using PowerCLI, the function returns a list of files that are present in a datastore (or a pipelined array of
datastores) that are not claimed by any of the virtual machines that are registered in vCenter as using 
this datastore.  Use the verbose parameter to get information about all files in the datastore.

.PARAMETER Datastore
Datastore object representing the datastore to search for unused files

.EXAMPLE
Get-Datastore -Datacenter (Get-Datacenter "DC1") | Get-DatastoreSpareFiles

All files that are not in use by a registered virtual machine are output.

.EXAMPLE
Get-DatastoreSpareFiles -Datastore:(Get-Datastore "local:host1")

Returns those files in datastore named "local:host1" that are not in use by a registered virtual machine and include details
of all files in the datastore in the output as well as whether the datastore is contactable.

.EXAMPLE
$dslist | Get-DatastoreSpareFiles

DatastoreFullPath                                                        Length
-----------------                                                        ------
[local:10.1.1.1] Guestserver-Admin - ...                                    338
....

.EXAMPLE
$dslist[0]| Get-DatastoreSpareFiles -verbose

VERBOSE: 33 datastores passed to Get-DatastoreSpareFiles
VERBOSE: Datastore local:10.1.1.1 is accessible
VERBOSE: 22/05/2012 15:36:37 Get-View Started execution
VERBOSE: 22/05/2012 15:36:38 Get-View Finished execution
VERBOSE: There are VirtualMachine-vm-0000 VirtualMachine-vm-0000
VirtualMachine-vm-0000 VirtualMachine-vm-0000 virtual machine(s) in the datastore local:10.1.1.1
[local:10.1.1.1] Guestserver-Admin - ...                                    338=
...
#›

function Get-DatastoreSpareFiles {
    Param (
		[Parameter(
			Mandatory=$True,
			ValueFromPipeline=$True,
			position=1)]
		[VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl[]] $Datastore,
		[switch] $HumanReadable
		)
	# (C) Mark Harrison, 2012
	# Find files that are not in use in a Datacenter
	# Unregistered virtual machines, media files, etc
	PROCESS {
		Write-Verbose "$($ds.length) datastores passed to Get-DatastoreSpareFiles"
		foreach ($ds in $Datastore) {
			if ($ds.Accessible) {
				Write-Verbose "Datastore $($ds.Name) is accessible"
				$dsView = Get-View $ds
				
				#get the VMs registered to be in this datastore
				$VMinDs = $dsView.VM
				$VM = @()
				$VMFilesinDatastore = @()
				foreach ($tempVM in $VMinDs) {
					Write-Verbose "There are $($VMinDs) virtual machine(s) in the datastore $($ds.Name)"
					$VM = Get-View $tempVM
					$Layout = $VM.LayoutEx
					foreach ($file in $Layout.File) { $VMFilesinDatastore += $file.Name }
					}
				Write-Verbose "The datastore $($ds.Name) should contain $($VMFilesinDatastore.length) file(s)"
				
				# Browse datastore
				$dsBrowser = Get-View $dsView.Browser
				$dsPath = $ds.DatastoreBrowserPath
				$FilesinDatastore = Get-ChildItem -Path:$dsPath -Recurse -Force
				Write-Verbose "The datastore $($ds.Name) contains $($FilesinDatastore.length) file(s)"
				
				#Compare files against VM files
				$SpareFiles = @()
				foreach ($file in $FilesinDatastore) {
					if ((!($file.ItemType -eq "Folder")) -and (!($VMFilesinDatastore -contains $file.DatastoreFullPath))) {
						if (!$HumanReadable) { 
							$SpareFiles += $file | select DatastoreFullPath,Length
						}
						else {
							$tempObj = "" | select DatastoreFullPath,Length
							$tempObj.DatastoreFullPath = $file.DatastoreFullPath
							$tempObj.Length = $file.Length | Get-BestValue
							$SpareFiles += $tempObj
							}
						}
					elseif ($file.ItemType -eq "Folder") {
						Write-Verbose "$($file.DatastoreFullPath) is a folder"
						}
					else {
						Write-Verbose "$($file.DatastoreFullPath) is in use by a registered virtual machine"
						}
					}
				$SpareFiles
				}
			else {
				Write-Error "Datastore $($ds.Name) is not accessible from here"
				}
			}
		}
	}

5 thoughts on “VMware datastores can hide many misdemeanours

  1. Hello! Do you know if they make any plugins to help with
    Search Engine Optimization? I’m trying to get my blog
    to rank for some targeted keywords but I’m not seeing very
    good results. If you know of any please share. Thank you!

  2. Very great post. I simply stumbled upon your weblog and wished to say that I have really enjoyed browsing your weblog posts. In any case I will be subscribing on your feed and I am hoping you write once more soon!

Leave a comment