13 Replies Latest reply on Apr 3, 2020 10:22 PM by ganapa2000

    Validate free space on destination datastore

    ganapa2000 Hot Shot

      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

         }

      }

        • 1. Re: Validate free space on destination datastore
          LucD Guru
          Community WarriorsvExpertUser Moderators

          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?
          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Validate free space on destination datastore
            ganapa2000 Hot Shot

            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.

            • 3. Re: Validate free space on destination datastore
              sarikrizvi Enthusiast

              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.

              • 4. Re: Validate free space on destination datastore
                LucD Guru
                vExpertUser ModeratorsCommunity Warriors

                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"

                    }

                }

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: Validate free space on destination datastore
                  ganapa2000 Hot Shot

                  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
                    LucD Guru
                    Community WarriorsvExpertUser Moderators

                    Try sorting on UsedSpaceGB instead of ProvisionedSpaceGB.

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    • 7. Re: Validate free space on destination datastore
                      ganapa2000 Hot Shot

                      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
                        ganapa2000 Hot Shot

                        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
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

                          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}

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: Validate free space on destination datastore
                            ganapa2000 Hot Shot

                            LucD,

                             

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

                             

                            • 11. Re: Validate free space on destination datastore
                              LucD Guru
                              User ModeratorsCommunity WarriorsvExpert

                              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

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 12. Re: Validate free space on destination datastore
                                LucD Guru
                                vExpertUser ModeratorsCommunity Warriors

                                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

                                 

                                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 13. Re: Validate free space on destination datastore
                                  ganapa2000 Hot Shot

                                  That worked perfectly. Thank you very LucD.