Search code examples
powershellvmwarepowercli

How to get vmware vm list which has more than 25% of used space of the particular datastore using powershell?


I need to get vm list which has 25% of the used space in the particular datastore.

$vmdatastore = Get-Datastore "XXXXX-LUN001" | select Name, @{N = Capacity";E={[math]::Round($_.ExtensionData.Summary.Capacity / 1GB, 2)}},@{N="FreeSpace";E={[math]::Round($_.ExtensionData.Summary.FreeSpace/1GB, 2)}}, @{N="PercentageFreeSpace";E={[math]::Round(($_.FreeSpaceGB)  /($_.CapacityGB) * 100, 2)}} 

$vmlists=Get-Datastore "XXXXX-LUN001" | Get-VM | Select name, @{N="DSName";E={$vmdatastore.Name}},usedspacegb,  provisionedspacegb 

Help me to get vm list which has 25% of used space in the particular datastore.. Thanks a lot in advance


Solution

  • Not quite sure, if that is what you want, but if you want a list of VMs that together take up at least 25% of the capacity of your datastore, this approach might help:

    $ds = Get-Datastore "XXXXX-LUN001"
    $dsVMs = Get-VM -Datastore $ds
    $vmUsedSpace = 0
    $vmMigrationCandidates = @()
    $dsCapacityThreshold = $ds.CapacityGB / 4
    
    while ($vmUsedSpace -le $dsCapacityThreshold -and $dsVMs.Count -gt 0)
    {
        $vm,$dsVMs = $dsVMs
        $vmUsedSpace += $vm.UsedSpaceGB
        $vmMigrationCandidates += $vm
    }
    

    $vmMigrationCandidates will hold a list of VMs that together use up at least 25% of the datastores total capacity. (Or all vms on this datastore, if they together dont reach the threshold.)

    You still need to decide, if you prefer to migrate a few "big" vms instead of many "small" ones. Also take into account, that there might be DRS host- or vm-affinity rules that you have to take into account.