vmware – finding orphaned files

Use following script for finding orphaned files in your vmware env…


#
# Purpose : List all orphaned vmdk on all datastores in all VC's
# Version: 1.1
# Author : HJA van Bokhoven
# Modifications: LucD

$arrayVC = @(“server”)
$report = @()

foreach ($strVC in $arrayVC)
{
Connect-VIServer $strVC
$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}
$arrDS = Get-Datastore | Sort-Object -property Name
foreach ($strDatastore in $arrDS)
{
Write-Host $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.matchPattern = "*.vmdk"
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $ds.browser
$rootPath = "["+$ds.summary.Name+"]"

#Workaround for vSphere 4 fileOwner bug
if ($dsBrowser.Client.Version -eq "Vim4") {
$searchSpec = [VMware.Vim.VIConvert]::ToVim4($searchSpec)
$searchSpec.details.fileOwnerSpecified = $true
$dsBrowserMoRef = [VMware.Vim.VIConvert]::ToVim4($dsBrowser.MoRef);
$searchTaskMoRef = $dsBrowser.Client.VimService.SearchDatastoreSubFolders_Task($dsBrowserMoRef, $rootPath, $searchSpec)
$searchResult = [VMware.Vim.VIConvert]::ToVim($dsBrowser.WaitForTask([VMware.Vim.VIConvert]::ToVim($searchTaskMoRef)))
} else {
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
}

foreach ($folder in $searchResult)
{
foreach ($fileResult in $folder.File)
{
if ($fileResult.Path)
{
if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){
$row = "" | Select DS, Path, File, Size, ModDate, Host
$row.DS = $strDatastore.Name
$row.Path = $folder.FolderPath
$row.File = $fileResult.Path
$row.Size = $fileResult.FileSize
$row.ModDate = $fileResult.Modification
$row.Host = (Get-View $ds.Host[0].Key).Name
$report += $row
}
}
}
}
}
# Disconnect session from VC
disconnect-viserver -confirm:$false
}

$report | Export-Csv "C:\VMDK-orphaned.csv" -noTypeInformation

4 Replies to “vmware – finding orphaned files”

  1. Any help? Get the following error:

    Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null
    .
    At C:\Users\ebowser\Desktop\findorphans.ps1:65 char:21
    + $report | Export-Csv <<<< "C:\VMDK-orphaned.csv" -noTypeInformation
    + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingV
    alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
    icrosoft.PowerShell.Commands.ExportCsvCommand

  2. Pingback: My Week in Geek – February 4, 2011 : My Geek Finds

  3. Hi, I’m getting these 2 errors running against vSphere 4x, can someone assist?

    Get-View : Cannot validate argument on parameter ‘VIObject’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Get-orphaned-VMDK-vSphere.ps1:24 char:24
    + $dsBrowser = Get-View <<<< $ds.browser
    + CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

    You cannot call a method on a null-valued expression.
    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Get-orphaned-VMDK-vSphere.ps1:35 char:56
    + $searchResult = $dsBrowser.SearchDatastoreSubFolders <<<< ($rootPath, $searchSpec)
    + CategoryInfo : InvalidOperation: (SearchDatastoreSubFolders:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Leave a Reply

Your email address will not be published.