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
}
}
Think I found the issue, you are using the $vm variable on the Move-VM.
That should be the pipeline object
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can find the free space on the target datastore with
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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.
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.
$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
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
}
}
Try sorting on UsedSpaceGB instead of ProvisionedSpaceGB.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
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.
Group-Object -Property {$_.GetType().Name}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
The Datastores are not in cluster and here is the requested output
Indeed, no SDRS events in there.
If you do the svMotion in the following way, does it also move the wrong one?
Move-VM -VM $vm -Datastore $destin
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Think I found the issue, you are using the $vm variable on the Move-VM.
That should be the pipeline object
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked perfectly. Thank you very LucD.