Skip to content

Used the API to improve the speed of "79 Find VMs in Uncontrolled Snapshot Mode" #770

@Legion87

Description

@Legion87

In some cases, it took us over 24 hours per environment to generate the report.
This meant that the process sometimes had to be interrupted or could only be run every 48 hours.

The API was integrated to solve the problem – have fun trying it out!

PS: I do not provide support for the script. But feel free to adapt it to your needs.

Before:
VMs in uncontrolled snapshot mode	13-14 hours

After:
VMs in uncontrolled snapshot mode	56 minutes
#>

# Start of Settings
$ExcludeDS = "local_vmfs"
# Exception for Xen-Desktop (-xd-)
$ExceptionFP = "-xd-|-ctk."
# End of Settings

# Variables for the results
$vmdks = @()
$items = @()

foreach ($Datastore in ($Datastores | Where-Object {$_.Name -notmatch $ExcludeDS -and $_.State -eq "Available"})) {
    $DatastoreBrowser = Get-View $Datastore.ExtensionData.Browser

    # Determine all visible folders in the datastore
    $Folders = Get-ChildItem -Path $Datastore.DatastoreBrowserPath -Force | Where-Object {
        # Do not search hidden folders
        $_.PSIsContainer -and $_.Name -notmatch '^\.'
    } | Sort-Object Name

    foreach ($Folder in $Folders) {
        # Create DatastorePath for each folder
        $DatastorePath = "[" + $Datastore.Name + "] /" + $Folder.Name

        # Define search patterns
        $SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
        # e.g. "*.vmdk" or "*-delta.vmdk"
        $SearchSpec.MatchPattern = @("*.vmdk")
        $SearchSpec.SearchCaseInsensitive = $true

        # Start search in specific folder
        $SearchResults = $DatastoreBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec)

        # Collect results
        foreach ($FolderResult in $SearchResults) {
            foreach ($File in $FolderResult.File) {
                if ($File.Path -notmatch $ExceptionFP) {
                    $items += [PSCustomObject]@{
                        Fullname   = $Datastore.DatastoreBrowserPath + '\' + $Folder.Name + '\' + $File.Path
                        FolderPath = $FolderResult.FolderPath.TrimEnd('/')
                    }
                }
            }
        }
    }
}
# Results
$vmdks += $items

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions