PowerCLI

 View Only
Expand all | Collapse all

get orphaned vmdk information from vSAN

LucD

LucDDec 02, 2020 07:38 AM

  • 1.  get orphaned vmdk information from vSAN

    Posted Dec 01, 2020 12:08 AM

    Hello Group, I see what appears to be a few orphaned vmdk files and folders in my datastore. Before deleting I was wanting to verify and hoping to find a way with PowerCLI (or optionally any method!). Most discussions point to LucD's script https://www.lucd.info/2011/04/25/orphaned-files-and-folders-spring-cleaning/.

    However when I run this against my vSAN datastore it does nothing, no error, no results. Further investigation reveals this only works with VMFS. So any help to discover my orphaned data on vSAN much appreciated.



  • 2.  RE: get orphaned vmdk information from vSAN

    Posted Dec 01, 2020 08:11 AM

    Can you try the attached, updated version of that function?
    It should work with VSAN datastores and take Content Libraries into account.



  • 3.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 04:04 AM

    Thanks so much for responding, however it is still quickly returning to the prompt with no results or errors

    PS C:\scripts> .\Remove-OrphanedData -Datastore VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3
    PS C:\scripts>



  • 4.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 07:38 AM

    Seems to be working for me.



  • 5.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 05:19 PM

    Okay, well hmmm. I know I have orphans because this datastore only supports two Horizon Desktop pools, and there are a couple master OS images for those. But I am seeing some VMDK folders and files from a couple years ago that have long been replaced, and with modified dates from 2018.



  • 6.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 05:30 PM

    Can you comment out the line

    $disk.details.thin = $true

    and give it another try?

    If those VMDK are not of type Thin they were not returned in the script. 



  • 7.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 06:52 PM

    Sorry but same result. These orphaned files were from Windows 10 images that  most likely used the default storage policy which would be thin.



  • 8.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 07:01 PM

    Next step could be to check if the search actually returns VMDK.
    Does the following return results, including the VMDK that you suspect are orphaned?

    $ds = Get-Datastore -Name MyDS
    
    $flags = New-Object VMware.Vim.FileQueryFlags
    $flags.FileSize = $true
    $flags.FileType = $true
    $disk = New-Object VMware.Vim.VmDiskFileQuery
    $disk.details = New-Object VMware.Vim.VmDiskFileQueryFlags
    $disk.details.capacityKb = $true
    $disk.details.diskExtents = $true
    $disk.details.diskType = $true
    $disk.details.thin = $true
    $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
    $searchSpec.details = $flags
    $searchSpec.Query += $disk
    $searchSpec.sortFoldersFirst = $true
    $dsBrowser = Get-View $ds.ExtensionData.browser
    $rootPath = "[" + $ds.Name + "]"
    $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
    $searchResult


  • 9.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 07:03 PM

    When that does return orphaned VMDK, can you list in detail what the info says.
    You will have to substitute the index 'x' for the actual value of the entry.

    $searchResult[x] | Format-Custom


  • 10.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 10:09 PM

    Thanks, sorry to be so obtuse but exactly what value do I use? This is the result of an orphaned VM.

    Datastore-datastore-33 [VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3] cd38835b-7439-424f-4a75-246e96c7e8d0 {, , , }

    I tried using cd38835b-7439-424f-4a75-246e96c7e8d0 and the whole path but am returning errors. Thanks!



  • 11.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 06:16 AM

    The variable $searchResult should be an array.
    You can check how many elements are in the array by doing $searchResult.Count.
    Then find an entry that is an orphaned VMDK, note the position in the array (indexing starts at 0).
    Then list the full content of that entry with

    $searchResult[x] | Format-Custom

    You can attach the output of that line as a TXT file to the thread. 



  • 12.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 07:08 PM

    Oh, DUH, of course, had a simple review of using arrays. Unfortunately getting this error about "Missing type name"

    Could that be that all these variables are not strings? or.... 



    PS C:\> $searchResult [55] | Format-Custom
    At line:1 char:15
    + $searchResult [55] | Format-Custom
    + ~~~~
    Unexpected token '[55]' in expression or statement.
    At line:1 char:16
    + $searchResult [55] | Format-Custom
    + ~
    Missing type name after '['.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken



  • 13.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 07:10 PM

    Looks like you might have a space before the '['



  • 14.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 07:31 PM

    Sorry you have to school me!

    PS C:\> $searchResult[55] | Format-Custom

    class HostDatastoreBrowserSearchResults
    {
    Datastore =
    class ManagedObjectReference
    {
    Type = Datastore
    Value = datastore-33
    }
    FolderPath = [VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3] cd38835b-7439-424f-4a75-246e96c7e8d0
    File =
    [
    class VmDiskFileInfo
    {
    DiskType = VirtualDiskSparseVer2BackingInfo
    CapacityKb = 131072000
    HardwareVersion =
    ControllerType =
    DiskExtents =
    [
    [VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3]
    cd38835b-7439-424f-4a75-246e96c7e8d0/vsan:/2cdb855b-2433-3b31-988c-246e96c7e8d0
    ]

    Thin = True
    Encryption =
    Path = StudentBase-000004.vmdk
    FriendlyName =
    FileSize = 19868418048
    Modification =
    Owner =
    }
    class VmDiskFileInfo
    {
    DiskType = VirtualDiskFlatVer2BackingInfo
    CapacityKb = 131072000
    HardwareVersion =
    ControllerType =
    DiskExtents =
    [
    [VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3]
    cd38835b-7439-424f-4a75-246e96c7e8d0/vsan:/cf38835b-1833-3d05-3492-246e96c7e8d0
    ]

    Thin = True
    Encryption =
    Path = StudentBase.vmdk
    FriendlyName =
    FileSize = 26310868992
    Modification =
    Owner =
    }
    class VmDiskFileInfo
    {
    DiskType = VirtualDiskSparseVer2BackingInfo
    CapacityKb = 131072000
    HardwareVersion =
    ControllerType =
    DiskExtents =
    [
    [VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3]
    cd38835b-7439-424f-4a75-246e96c7e8d0/vsan:/5548835b-96cf-5a0b-26b7-246e96c7e8d0
    ]

    Thin = True
    Encryption =
    Path = StudentBase-000001.vmdk
    FriendlyName =
    FileSize = 28760342528
    Modification =
    Owner =
    }
    class VmDiskFileInfo
    {
    DiskType = VirtualDiskSparseVer2BackingInfo
    CapacityKb = 131072000
    HardwareVersion =
    ControllerType =
    DiskExtents =
    [
    [VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3]
    cd38835b-7439-424f-4a75-246e96c7e8d0/vsan:/f481845b-149d-c521-6f92-246e96c7e8d0
    ]

    Thin = True
    Encryption =
    Path = StudentBase-000002.vmdk
    FriendlyName =
    FileSize = 23366467584
    Modification =
    Owner =
    }
    ]

    LinkedView =
    }

     



  • 15.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 08:16 PM

    That looks ok, thanks.
    The next step is to look at the folders that contain valid VMs and Templates.
    Does the following code show the folder  [VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3] cd38835b-7439-424f-4a75-246e96c7e8d0 in the results?

    $fldList = @{}
    
    $ds = Get-Datastore -Name MyDS
    
    Get-VM -Datastore $ds | % {
        $_.Extensiondata.LayoutEx.File | where { "diskDescriptor", "diskExtent" -contains $_.Type } | % {
            $fldList[$_.Name.Split('/')[0]] = $_.Name
        }
    }
    Get-Template | where { $_.DatastoreIdList -contains $ds.Id } | % {
        $_.Extensiondata.LayoutEx.File | where { "diskDescriptor", "diskExtent" -contains $_.Type } | % {
            $fldList[$_.Name.Split('/')[0]] = $_.Name
        }
    }
    
    $fldList.Keys

     



  • 16.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 08:50 PM

    Bingo! No it does not, and the array count went from 190 to 113.



  • 17.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 09:31 PM

    Ok, so we established that there are indeed folders with orphaned VMDK.
    The next step is to find out why the function doesn't seem to find them.

    I attached a new version of the function, it contains some verbose output.
    Call the function now with

    Remove-OrphanedData -Datastore MyDS -Verbose

     This will probably produce a lot of output.
    Perhaps best to save it in a TXT file and attach it.



  • 18.  RE: get orphaned vmdk information from vSAN

    Posted Dec 03, 2020 10:02 PM

    This immediately returned to the prompt without error or output.



  • 19.  RE: get orphaned vmdk information from vSAN

    Posted Dec 04, 2020 08:03 AM

    I find that hard to believe.
    Did you use the Verbose switch on the call to the function?
    How in fact are you running this? In a .ps1 file?



  • 20.  RE: get orphaned vmdk information from vSAN

    Posted Dec 04, 2020 06:15 PM

    Not sure, what can I be doing wrong?


    PS C:\scripts> .\Remove-OrphanedData -Datastore VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3 -Verbo
    se
    PS C:\scripts>
    PS C:\scripts> ls


    Directory: C:\scripts


    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a---- 12/3/2020 1:56 PM 6416 Remove-OrphanedData.ps1


    PS C:\scripts> Get-Datastore -name VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3

    Name FreeSpaceGB CapacityGB
    ---- ----------- ----------
    VxRail-Virtual-SAN-Datastor... 12,567.094 26,827.313


    PS C:\scripts>



  • 21.  RE: get orphaned vmdk information from vSAN

    Posted Dec 04, 2020 06:33 PM

    Depends what you have in that .ps1 file?
    Is that the complete function (first line says Function)?
    Or did you convert it to a plain script?

    The easiest way to call the function would be to copy the full function (the file I attached earlier) into a .ps1 file.
    Then in that same .ps1 file, at the end, add a new line calling the function.
    In your case that would be

    Remove-OrphanedData -Datastore VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3 -Verbo
    se

    Now just run the .ps1 file, without any arguments.



  • 22.  RE: get orphaned vmdk information from vSAN

    Posted Dec 04, 2020 07:52 PM

    The first time I simply extracted the script from the zip and used as is. Seems to work most of the time LOL!  This time I simply opened it it in notepad++, added the line to the bottom and saved, and it ran. Thanks for your patience. I used out-file to get into text.



  • 23.  RE: get orphaned vmdk information from vSAN
    Best Answer

    Posted Dec 04, 2020 09:45 PM

    So it is working?
    It returns all the orphaned VMDK?



  • 24.  RE: get orphaned vmdk information from vSAN

    Posted Dec 13, 2020 05:11 PM

    LucD,

    Sorry for the delay, I got married and went on a little vacation. Thought I had responded to that effect but do not see it now. Anyway, I believe you nailed it, this looks good. Because this is a Horizon Linked Clone deployment, there are a lot more results than I believe there would be from persistant VM's. So I need to analyze a bit more before I would run a delete. Be curious if you have any experience running the script against Horizon non-persistant VM's. If I find anything that suggests an invalid rsult I will be sure to circle back and let you know. Meanwhile thank you for all your time and patience.

     



  • 25.  RE: get orphaned vmdk information from vSAN

    Posted Dec 13, 2020 05:21 PM

    No problem, and congratulations to both of you. 

    Unfortunately, I don't have any experience with non-persistent VM on VSAN storage.



  • 26.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 08:07 AM

    Can you check what this returns for your datastore?

    Get-Datastore -Name MyDS |
    Select Name,Type,@{N='Multi';E={$_.ExtensionData.Summary.MultipleHostAccess}}


  • 27.  RE: get orphaned vmdk information from vSAN

    Posted Dec 02, 2020 05:14 PM

    PS C:\> Get-Datastore -Name VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3 |
    >> Select Name,Type,@{N='Multi';E={$_.ExtensionData.Summary.MultipleHostAccess}}

    Name Type Multi
    ---- ---- -----
    VxRail-Virtual-SAN-Datastore-0cc5f2fb-e267-4c11-af7b-9e26443ee9a3 vsan True