Automation

 View Only
Expand all | Collapse all

Validate free space on destination datastore

  • 1.  Validate free space on destination datastore

    Posted Apr 03, 2020 03:07 PM

    Hi,

    Please help me, how can I validate the free space on the destination datastore before I start the storage vmotion on the VM

    $source = "DS03"

    $destin = "DS39"

    Get-Datastore $source | Get-VM -PipelineVariable vm |

    ForEach-Object -Process {

       $task = Move-VM -VM $vm -Datastore $destin -RunAsync

       while ($task.PercentComplete -ne 100)

       {

      sleep 5

       $task = Get-Task -Id $task.Id

       $sProg = @{

         Activity = "$vm Storage vMotion from $source to $destin"

         Status = "$($task.PercentComplete)%"

         PercentComplete = ([int]($task.PercentComplete))

       }

       Write-Progress @sProg

       }

    }



  • 2.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 03:48 PM

    You can find the free space on the target datastore with

    Get-Datastore -Name $destin

    But what exactly would be the condition you want to test for?
    The size of the VM you are moving?


  • 3.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 03:59 PM

    Hi LucD,

    I wanted to ensure that destination datastore has 20% of free space of total datastore space.

    For example, if it is a 4 TB datastore then I need to make sure that it has 20% free space available to migrate the VM.



  • 4.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 04:18 PM

    You could do something like this.
    You can eventually ut a break in the else-block, so this message doesn't repeat for each VM still in the pipeline.

    $source = "DS03"

    $destin = "DS39"

    Get-Datastore $source | Get-VM -PipelineVariable vm |

    ForEach-Object -Process {

        $destDS = Get-Datastore $destin

        if ($destDS.FreeSpaceGB / $destDS.CapacityGB -gt 0.2) {

            $task = Move-VM -VM $vm -Datastore $destin -RunAsync

            while ($task.PercentComplete -ne 100) {

                Start-Sleep 5

                $task = Get-Task -Id $task.Id

                $sProg = @{

                    Activity        = "$vm Storage vMotion from $source to $destin"

                    Status          = "$($task.PercentComplete)%"

                    PercentComplete = ([int]($task.PercentComplete))

                }

                Write-Progress @sProg

            }

        }

        else {

            Write-Host "Destination doesn't have 20% of free space"

        }

    }



  • 5.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 04:41 PM

    LucD,

    I tried test as below to move one small VM to different datastore, but it started to move a bigger size VM randomly,

    Please help, whats wrong with the below

    $source = "DS03"

    $destin = "DS39"

    Get-Datastore $source | Get-VM -PipelineVariable vm | Sort-Object -Property ProvisionedSpaceGB | Select -First 1 |

    ForEach-Object -Process {

        $destDS = Get-Datastore $destin

        if ($destDS.FreeSpaceGB / $destDS.CapacityGB -gt 0.15) {

            $task = Move-VM -VM $vm -Datastore $destin -RunAsync

            while ($task.PercentComplete -ne 100) {

                Start-Sleep 5

                $task = Get-Task -Id $task.Id

                $sProg = @{

                    Activity        = "$vm Storage vMotion from $source to $destin"

                    Status          = "$($task.PercentComplete)%"

                    PercentComplete = ([int]($task.PercentComplete))

                }

                Write-Progress @sProg

            }

        }

        else {

            Write-Host "Destination doesn't have 20% of free space"

            break

        }

    }



  • 6.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 04:44 PM

    Try sorting on UsedSpaceGB instead of ProvisionedSpaceGB.



  • 7.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 04:48 PM

    LucD,

    When I use the below line directly, it shows me the small VM name but when I use with the script, it used some random bigger VM

    Get-Datastore $source | Get-VM -PipelineVariable vm | Sort-Object -Property ProvisionedSpaceGB | Select -First 1



  • 8.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 05:09 PM

    LucD,

    I tried using below but still, it is not using the smallest VM, it is using 3 smallest VM

    Get-Datastore $source | Get-VM -PipelineVariable vm | Sort-Object -Property UsedSpaceGB | Select -First 1

    My output from existing datastore for smallest VMs

    Get-Datastore DS03 | Get-VM -PipelineVariable vm | Sort-Object -Property UsedSpaceGB | Select Name, UsedSpaceGB -First 5

    Name                          UsedSpaceGB

    ----                          -----------

    veerDS  40.000008087605237960815429688

    mytestvm     40.0021863281726837158203125

    mytestvm10 44.081261946819722652435302734

    Mem1     54.103143965825438499450683594

    rpt1       59.1003954112529754638671875

    When I made the changes as Get-Datastore $source | Get-VM -PipelineVariable vm | Sort-Object -Property UsedSpaceGB | Select -First 1 in the script, instead of migrating veerDS VM, it started to migrate mytestvm10



  • 9.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 05:21 PM

    If you only select 1 VM for an svMotion, it should only svMotion that VM.
    But, it could be that SDRS is triggering that svMotion.
    Is that target datastore in a DatastoreCluster?

    Perhaps SDRS decided that it first had to move that mytestvm10 VM?
    The only to know for sure is to look at the events, and find the one that handles that svMotion.

    Get-VIEvent -Start (Get-Date).AddMinutes(-5) -MaxSamples ([int]::MaxValue) |

    Group-Object -Property {$_.GetType().Name}



  • 10.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 05:44 PM

    LucD,

    The Datastores are not in cluster and here is the requested output



  • 11.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 05:53 PM

    Indeed, no SDRS events in there.

    If you do the svMotion in the following way, does it also move the wrong one?

    $vm = Get-Datastore $source | Get-VM | Sort-Object -Property ProvisionedSpaceGB | Select -First 1

    Move-VM -VM $vm -Datastore $destin



  • 12.  RE: Validate free space on destination datastore
    Best Answer

    Posted Apr 03, 2020 05:55 PM

    Think I found the issue, you are using the $vm variable on the Move-VM.
    That should be the pipeline object

    $task = Move-VM -VM $_ -Datastore $destin -RunAsync



  • 13.  RE: Validate free space on destination datastore

    Posted Apr 04, 2020 05:23 AM

    That worked perfectly. Thank you very LucD. :smileyhappy:



  • 14.  RE: Validate free space on destination datastore

    Posted Apr 03, 2020 04:02 PM

    If there is no sufficient free space available on destination data-store in that case storage vmotion will not happen/start on that data-store.