VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Storage vMotion

Hi,

I am trying to migrate VMs from one Datastore to another using below command, when I issue this command, all the VMs are migrating at once, I would like to migrate one VM at a time, How can I do it ?

Please help!!!

get-datastore Datastore1 | get-vm | move-vm -Datastore Datastore2

Below are the PowerCLI version installed

PowerCLI Version

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

   VMware PowerCLI 11.2.0 build 12483598

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

Component Versions

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

   VMware Cis Core PowerCLI Component PowerCLI Component 11.2 build 12483642

   VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.2 build 12483638

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are using the incorrect parameters.

PercentComplete expects an [int], you can pass your text in Status.
Like this

Get-Datastore Datastore1 | Get-VM -PipelineVariable vm |

ForEach-Object -Process {

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

   while ($task.PercentComplete -ne 100)

   {

  sleep 5

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

   $sProg = @{

     Activity = "Storage vMotion"

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

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

   }

   Write-Progress @sProg

   }

}


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

You could run the svMotion as a background task, and then wait till it is completed, before starting the next one.

Get-Datastore Datastore1 | Get-VM -PipelineVariable vm |

ForEach-Object -Process {

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

   while ($task.PercentComplete -ne 100)

   {

  sleep 5

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

   }

}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thanks LucD for your help.

That worked. Just wanted to know, when do we use -RunAsync parameter ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The RunAsync switch starts the task corresponding with the cmdlet in the background.
The cmdlet returns immediately without waiting for the background task to complete.
With the Task object, that is returned when using RunAsync, and Get-Task you can monitor the state of the Task.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Got it,

But in the script, does the below line has anything to do ? because,I dont see any task status in the screen ?

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

We have the Task object in that variable, but such an object does not refresh automatically.
That is why we have to refresh the content of the object, to get the actual status


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD, how do I refresh to get the actual status ? sorry to bother you.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I use the Id with the Get-Task cmdlet to get the current content of the Task object.
That also includes the property that shows what percentage of the task is completed.
At 100% we continue with the next svMotion


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Sorry, if I misunderstood this....just wanted to know, if there is a way to display the percentage status in the CLI regarding the task ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, after the Get-Task -Id $task.Id, the actual percentage is available in the PercentComplete property.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I added the below line

Get-Datastore Datastore1 | Get-VM -PipelineVariable vm |

ForEach-Object -Process {

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

   while ($task.PercentComplete -ne 100)

   {

  sleep 5

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

   Write-Progress -Activity "Storage vMotion :" -PercentComplete "$($task.PercentComplete)%"

   }

}

From the output, number percentage is missing

Output :

pastedImage_3.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are using the incorrect parameters.

PercentComplete expects an [int], you can pass your text in Status.
Like this

Get-Datastore Datastore1 | Get-VM -PipelineVariable vm |

ForEach-Object -Process {

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

   while ($task.PercentComplete -ne 100)

   {

  sleep 5

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

   $sProg = @{

     Activity = "Storage vMotion"

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

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

   }

   Write-Progress @sProg

   }

}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Perfect....enjoying.....new learning everyday because of you...thank you very much Smiley Happy