1 2 3 Previous Next 72 Replies Latest reply on Oct 21, 2015 6:50 AM by LucD

    orphaned vmdk files script

    Guv Hot Shot

      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. Re: orphaned vmdk files script
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          The $rootPath variable was incorrect.

          Attached a corrected version.

           

           

           

           

          ____________

          Blog: LucD notes

          Twitter: lucd22

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: orphaned vmdk files script
            Guv Hot Shot

            thanks for that lucd.  The script is running now.  Its listing all datastores very slowly, so I am assuming that there are no orphaned vmdk files if it just lists the datatstores. otherwise it would show it up under the datastore.  is that correct ?

            • 3. Re: orphaned vmdk files script
              LucD Guru
              vExpertUser ModeratorsCommunity Warriors

              That is correct.

               

               

               

               

              ____________

              Blog: LucD notes

              Twitter: lucd22

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: orphaned vmdk files script
                vwman Novice

                 

                Is there a way to have the script ignore local datastores?  All of my local datastores contain the word "local".

                 

                 

                Thanks

                 

                 

                Scott

                 

                 

                • 5. Re: orphaned vmdk files script
                  LucD Guru
                  vExpertCommunity WarriorsUser Moderators

                  Yes, if you change the 3th line in my script into this

                  ...
                  $arrDS = Get-Datastore | where {$_.Name -notlike "*local*"} | Sort-Object -property Name
                  ...
                  

                   

                   

                   

                   

                   

                  ____________

                  Blog: LucD notes

                  Twitter: lucd22

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: orphaned vmdk files script
                    vwman Novice

                     

                    Perfect.

                     

                     

                    Thanks!

                     

                     

                    • 7. Re: orphaned vmdk files script
                      karlgag Novice

                      Hi LucD,

                       

                      What about vm's with snapshot ? The vm property point on the last snapshot (myVmName-00000X.vmdk) and your script will show an orphaned vmdk for myVmName.vmdk. I challenge you because my script look like yours but I can't find a property who can show me the disk root parent.

                       

                      I work with something like this =>  if ( $disk.Filename -like "00000?" )...

                      So, If you have another suggestion, I will be happy  )

                       

                      Thanks 

                       

                      Karl

                      • 8. Re: orphaned vmdk files script
                        LucD Guru
                        User ModeratorsvExpertCommunity Warriors

                        I would suggest to use the layoutEx property in the VirtualMachine object to find the snapshot root.

                         

                        Kinda like I did in my yadr – A vdisk reporter post.

                         

                        In my opinion you can't rely on the filename alone.

                        I have seen VMs where this doesn't follow anymore what you would expect.

                         

                         

                         

                        ____________

                        Blog: LucD notes

                        Twitter: lucd22

                        Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        • 9. Re: orphaned vmdk files script
                          KSemea Lurker

                          Hi there,

                           

                          I have run your script which completes sucsessfully.

                           

                          But no output is generated. Im sure I have orphaned files does it writh the results to a textfile anywhere..?

                           

                          Kr

                          Paul 

                          • 10. Re: orphaned vmdk files script
                            LucD Guru
                            User ModeratorsvExpertCommunity Warriors

                            The yadr script stores the result in a CSV file called "C:\Yadr-report.csv"

                            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 11. Re: orphaned vmdk files script
                              Cy5 Novice

                              Hi, I get errors when running against vSphere 4.x (ESXi 4.x): does this script work in that environment?

                               

                               

                              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

                              • 12. Re: orphaned vmdk files script
                                LucD Guru
                                User ModeratorsCommunity WarriorsvExpert

                                Are you connected to the ESXi server or to a vCenter ?

                                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 13. Re: orphaned vmdk files script
                                  Cy5 Novice

                                  Vcenter server, not directly to ESXi

                                  • 14. Re: orphaned vmdk files script
                                    LucD Guru
                                    User ModeratorsCommunity WarriorsvExpert

                                    What type of datastore was being handled at the time of the error ?

                                    The name should have been printed to the console.

                                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                    1 2 3 Previous Next