1 2 3 5 Previous Next 62 Replies Latest reply: Jan 30, 2015 9:26 AM by LucD RSS

orphaned vmdk files script

Guv Hot Shot
Currently Being Moderated

I found the below useful script for finding orphaned vmdk files as seen below:

 

  1. Purpose : List all orphaned vmdk on all datastores in all VC's

  2. Version: 1.1

  3. Author  : HJA van Bokhoven

  4. Modifications: LucD

 

$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 = ""

          $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

                         }

                    }

               }

          }

     }

 

 

When I run this script, it lists my datastores, but then after each datastore it brings up the following error message:

 

Exception calling "SeachDatastoresubfolders" with "2" argument (s): Invalid datastore path..........

 

Is there any reason why this script would bring this error message, I am using the lastest version of powercli client, or would it mean I have no orphaned vmdk files.  Any advise.

1 2 3 5 Previous Next

More Like This

  • Retrieving data ...

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 6 points

Share This Page