Get List of VMDK files on Datastore

Version 1

    Hi  i have got this piece of code that will list all VMDK files on a datastore weather attach to a VM or not, thought it might be useful for someone

     

    $arrDS = Get-Datastore xxxxx

    $strDatastore = $arrDS

    Foreach ($strDatastore in $arrDS)

    {

    $strDatastoreName = $strDatastore.name

    $ds = Get-Datastore -Name $strDatastoreName | %{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.sortFoldersFirst = $true

    $dsBrowser = Get-View $ds.browser

    $rootPath = "["+$ds.summary.Name+"]"

    $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

    $myCol = @()

     

     

    foreach ($folder in $searchResult)

    {

    foreach ($fileResult in $folder.File)

    {

    $file = "" | select Name, FullPath

    $file.Name = $fileResult.Path

     

    $strFilename = $file.Name

    IF ($strFilename)

    {

    IF ($strFilename.Contains(".vmdk"))

    {

    $fldrPath = $searchResult | select * | where {$_.folderpath -match "carmel-a-paw04"}

    $fldrPath.FolderPath + $file.Name

    IF (!$strFilename.Contains("-flat.vmdk"))

    {

    IF (!$strFilename.Contains("delta.vmdk"))

    {

    $strCheckfile = "*"+$file.Name+"*"

    IF ($arrUsedDisks -Like $strCheckfile){}

    ELSE

    {

    #$strOutput = $strDatastoreName + " Orphaned VMDK Found: " + $strFilename

    #$strOutput

    }

    }

    }

    }

    }

    }

    }

    }