VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Validate free space on destination datastore

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

   }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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.

Reply
0 Kudos
sarikrizvi
Enthusiast
Enthusiast
Jump to solution

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.

Regards,
SARIK (Infrastructure Architect)
vExpert 2018-2020 | vExpert - Pro | NSX | Security
vCAP-DCD 6.5 | vCP-DCV 5.0 | 5.5 | 6.0 | vCA-DCV 5 | vCA-Cloud 5 | RHCSA & RHCE 6 | A+ (HW & NW)
__________________
Please Mark "Helpful" or "Correct" if It'll help you
_____________________________________
@Follow:
Blog# https://vmwarevtech.com
vExpert# https://vexpert.vmware.com/directory/1997
Badge# https://www.youracclaim.com/users/sarik
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

    }

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try sorting on UsedSpaceGB instead of ProvisionedSpaceGB.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

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

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

That worked perfectly. Thank you very LucD. Smiley Happy

Reply
0 Kudos