Skip to content

Instantly share code, notes, and snippets.

@Nothing4You
Last active August 7, 2022 18:38
Show Gist options
  • Save Nothing4You/2512b7cc226f8cf1c7edfb49b6faa40a to your computer and use it in GitHub Desktop.
Save Nothing4You/2512b7cc226f8cf1c7edfb49b6faa40a to your computer and use it in GitHub Desktop.
Find orphaned VMDKs in VMware vSphere
# Don't forget to run Connect-VIServer before running this script
# Tested against vSphere 6.7 using PSCore 7
# Function from https://theposhwolf.com/howtos/Format-Bytes/
# https://github.com/ThePoShWolf/Utilities/blob/88860fefb1e35a6f71e9799e1a51ae903d074f8d/Misc/Format-Bytes.ps1
# under MIT license: https://github.com/ThePoShWolf/Utilities/blob/88860fefb1e35a6f71e9799e1a51ae903d074f8d/LICENSE
Function Format-Bytes {
Param
(
[Parameter(
ValueFromPipeline = $true
)]
[ValidateNotNullOrEmpty()]
[float]$number
)
Begin{
$sizes = 'KB','MB','GB','TB','PB'
}
Process {
for($x = 0;$x -lt $sizes.count; $x++){
if ($number -lt [int64]"1$($sizes[$x])"){
if ($x -eq 0){
return "$number B"
} else {
$num = $number / [int64]"1$($sizes[$x-1])"
$num = "{0:N2}" -f $num
return "$num $($sizes[$x-1])"
}
}
}
}
End{}
}
# based on http://www.vmwareadmins.com/finding-orphaned-vmdks-using-powercli/
$arrUsedDisks = Get-VM | Get-HardDisk | %{$_.filename}
$arrUsedDisks += Get-Template | Get-HardDisk | %{$_.filename}
Write-Host "Found $($arrUsedDisks.Count) disks referenced by VMs or templates"
$arrDS = Get-Datastore
Write-Host "Found $($arrDS.Count) datastores"
$totalOrphanedSize = 0
Foreach ($strDatastore in $arrDS)
{
Write-Host "Checking datastore $($strDatastore.name)"
$ds = Get-Datastore -Name $strDatastore.name | %{Get-View $_.Id}
$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
$fileQueryFlags.FileSize = $true
$fileQueryFlags.FileType = $true
$fileQueryFlags.Modification = $true
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $fileQueryFlags
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $ds.browser
$rootPath = "[$($ds.summary.Name)]"
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
$dsTotalOrphanedSize = 0
foreach ($folder in $searchResult)
{
foreach ($fileResult in $folder.File)
{
If (
$fileResult.Path -and
$fileResult.Path.EndsWith(".vmdk") -and
!$fileResult.Path.EndsWith("-flat.vmdk") -and
!$fileResult.Path.EndsWith("delta.vmdk") -and
!$fileResult.Path.EndsWith("-ctk.vmdk") -and
!($arrUsedDisks -contains "$($folder.FolderPath)$($fileResult.Path)")
) {
$dsTotalOrphanedSize += $fileResult.FileSize
Write-Host "Orphaned VMDK Found: $($folder.FolderPath)$($fileResult.Path) ($($fileResult.FileSize | Format-Bytes))"
}
}
}
$totalOrphanedSize += $dsTotalOrphanedSize
Write-Host "[$($ds.summary.Name)] Found $($dsTotalOrphanedSize | Format-Bytes) of orphaned disks"
}
Write-Host "Found $($totalOrphanedSize | Format-Bytes) of orphaned disks across all datastores"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment