Please enable Javascript for better experience...
 
Powershell Script to get Orphaned VMDK
By Pranay Jha | May 4, 2016 | In Tips | Total Views [ 1713 ]
Taged In
(0 Like)
Rate

$report = @()
$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.Name + "]"
    $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
                    $row.DS = $strDatastore.Name
                    $row.Path = $folder.FolderPath
                    $row.File = $fileResult.Path
                    $row.Size = $fileResult.FileSize
                    $row.ModDate = $fileResult.Modification
                    $report += $row
                }
            }
        }
    }
}


Thanks for visiting here. Share this article if you found it useful.
Join Facebook Group https://www.facebook.com/groups/VMwareInsight/
Like Facebook Page https://www.facebook.com/VMwareInsight/
Connect to twitter https://twitter.com/VMwareInsight
Share this on Social Media

About the Author

Pranay Jha
Pranay Jha
Founder, Contributer VMwareInsight.com
Follow me

facebook linkedin twitter G+ VMTN

My name is Pranay Jha, working in IBM as Technical Solution Architect for Virtualization platform. I have been working in IT for near around 10 years. Passionate to get involved as an Independent blogger for my blog http://vmwareinsight.com. I am VMware vExpert 2016/2017, VCAP5-DCD/DCA, VCP5-DCV, VCA-Cloud, VCE-CIA, MCSE, MCITP, MCSA(Messaging). For any query or suggestion, please connect me at pranay1988jha@gmail.com. Thanks for visiting here.

 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
Sponsors
 
 
 
 
 
Facebook Likes