VMware Cloud Community
marius1
Enthusiast
Enthusiast
Jump to solution

Moving VSs to a different datastore

I need to create a job to move VMs to a different datastore.

I created a script like the following one:

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer x.y.z.t -User username

$target = "Datastore2"

$listaVM =

"VM1",

"VM2",

"VM3",

"VM4",

"VM5",

"VM6"

foreach ($vm in $listaVM) {

$vm

Get-VM -Name $vm | Move-VM -Datastore $target -DiskStorageFormat 'Thin'

}

The job looks to work but I need to improve it so that:

  • After issuing the command to process VM1 it waits till the move is complete
  • If VM2 cannot be moved for any reason the process goes on to process VM3
  • After processing each VM I get information about the state of the move (success, error, ...)

Can anybody please help?

Regards

marius

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

The RunAsync will start the move in the background, then the While-loop waits till the move is complete.

From the Task object the status of the move is obtained.

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer x.y.z.t -User username

$target = "Datastore2"

$listaVM =

"VM1",

"VM2",

"VM3",

"VM4",

"VM5",

"VM6"

foreach ($vm in Get-VM -Name $listaVM) {

    $job = Move-VM -VM $vm -Datastore $target -DiskStorageFormat 'Thin' -RunAsync

    while($job.PercentComplete -ne 100)

    {

        sleep 5

        $job = Get-Task | where{$_.Id -eq $job.Id}

    }

    Write-Host "$($job.Result) is migrated, status $($job.State)"

}


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

The RunAsync will start the move in the background, then the While-loop waits till the move is complete.

From the Task object the status of the move is obtained.

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer x.y.z.t -User username

$target = "Datastore2"

$listaVM =

"VM1",

"VM2",

"VM3",

"VM4",

"VM5",

"VM6"

foreach ($vm in Get-VM -Name $listaVM) {

    $job = Move-VM -VM $vm -Datastore $target -DiskStorageFormat 'Thin' -RunAsync

    while($job.PercentComplete -ne 100)

    {

        sleep 5

        $job = Get-Task | where{$_.Id -eq $job.Id}

    }

    Write-Host "$($job.Result) is migrated, status $($job.State)"

}


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

marius1
Enthusiast
Enthusiast
Jump to solution

Many thanks!

marius

Reply
0 Kudos