Orphaned Files Revisited

In my Orphaned files and folders – Spring cleaning post from way back, I provided a script to find orphaned VMDKs. This week there was a post in the VMTN PowerCLI Community that had a request to find all orphaned files. Time for a revisit of my old post!

 

file-orphan

I took my old script, massaged it a bit and gave it a more contemporary look and feel.
Just for info, the SearchDatastoreSubFolders method is relatively slow. So scanning a couple of datastores for orphaned files might take a bit of time. Be patient 🙂

The Script

Annotations

Line 28-57: The script constructs the SearchDatastoreSubFolders parameter in the Begin block. This parameter will be the same for all datastores.

Line 56: To get maximum detail for the returned files, the script uses all available FileQuery variations

Line 62-64: Cheap OBN implementation

Line 67: The script only looks at shared VMFS datastores

Line 71: This hash table will be used to determine which file/folder is orphaned or not. First all the files the method fins are placed in the hash table. Then the files that belong to VMs and Templates are removed. Finally some system files are removed. What is left are orphaned files.

Line 76: Get all the files on the datastore with the SearchDatastoreSubFolders method

Line 77-87: Enter the files in the hash table

Line 83-85: Take care of the folder entries. If a file inside a folder is encountered, the script also removes the folder itself from the hash table

Line 90-96: Remove all files that belong to registered VMs from the hash table

Line 99-105: Remove all files that belong to registered Templates from the hash table

Line 108-111: Remove system files. For now these all files that are located in folders that start with a dot, or in a folder named vmkdump.

Line 114-128: If there are entries left in the hash table, collect more information and create an ordered object containing that information.

Sample Usage

The use of this function is rather straightforward, call the function with a datastorename or a datastore object.

or

The function also accepts the datastore objects via the pipeline. You can do

The objects that are returned by the function, depending on the type of file, contain information about the file. The following sample output shows how some properties are specific to certain file types.

orphan-1

If you want to create an orphaned files report for multiple datastores, it is handy to capture the results for each datastore in a separate worksheet in an Excel spreadsheet.

The following sample code uses Doug Finke‘s ImportExcel module to accomplish that.

The resulting spreadsheet has all the orphaned files, with a separate worksheet for each datastore that has orphaned files.

orphan-2

Enjoy!

 

12 Comments

    JerH

    Thanks for taking the time and effort to create these scripts. I’m really new to using PS scripts in VMware. I’m assuming somewhere along the way, I need to specify which vCenter to run this on? Can you help with the proper language for that? Also, I get an error with I try to run it with the export-excel function. Do I just paste that code into the end of your script? Maybe I need to download Doug Finkel’s code to make it work? Sorry for the annoying newb questions, but I’m not getting anywhere trying to figure it out on my own…

    Dennis Zimmer

    Great Script Luc! I would change line 67 to avoid an error for datastores that can’t be accessed.
    if($ds.Type -eq “VMFS” -and $ds.ExtensionData.Summary.MultipleHostAccess -and $ds.Accessible){

    That script will definitely makes its way into our OpBot repository!

      LucD

      Great suggestion Dennis.
      The code has been updated.

    Dennis Zimmer

    Amazing script Luc, that will find its way into our OpBot function library. Small improvement, that the script runs through even if some datastores are not accessible:
    Line 61
    if($ds.Type -eq “VMFS” -and $ds.ExtensionData.Summary.MultipleHostAccess){

    I would change to
    if($ds.Type -eq “VMFS” -and $ds.ExtensionData.Summary.MultipleHostAccess -and $ds.Accessible){

    Todd Scalzott

    Thanks so much for this great script, Luc.

    Are you missing “$_.Group |” prior to “Export-Excel” in your sample utilizing the ImportExcel module?

    Thanks,
    Todd

      LucD

      Thanks Todd, your assumption is correct, well spotted.
      I updated the sample code.

    Stu Green

    Hi Luc,

    Thanks for this script 🙂

    I added this filter which I found to give the output in Byte\KB\MB\GB\TB\PB format

    https://mjolinor.wordpress.com/2012/04/18/powershell-byte-count-formatting-simplified/

    Cheers!

    Ritam9

    Hi Luc,
    Thanks again for your wonderful scripts. Is this has to be run from the Powershell only ? I am not able to run from powercli . Can you provide the script which can be run from the powercli. Also does it avoid snapshot files / ctk files ?

    PowerCLI D:\> powercliversion

    PowerCLI Version
    —————-
    VMware vSphere PowerCLI 5.1 Release 1 build 793510
    —————
    Snapin Versions
    —————
    VMWare AutoDeploy PowerCLI Component 5.1 build 768137
    VMWare ImageBuilder PowerCLI Component 5.1 build 768137
    VMware License PowerCLI Component 5.1 build 669840
    VMware vSphere PowerCLI Component 5.1 build 793489

      LucD

      Hi Ritam9,
      You have to download the function, under The Script, from the post, and store it in a .ps1 file.
      Now, at the end of the file, you will have to call the function. I have some examples of that in the Sample Usage.
      Once you have all that in the .ps1 file, save the file and from the PowerCLI prompt you can call the .ps1 file.
      Note that you will have to connect (Connect-VIServer) first to a vSphere Server (vCenter or ESXi node).
      I hope that helps.
      Luc

    Tony

    Thanks for another great script. Note, in your example section you have Get-VmwOrphaned -Datastore DS1, which should be updated to Get-VmwOrphan

      LucD

      Thanks Tony.
      And thanks for spotting that!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Buy the Book