Automation

 View Only
  • 1.  Moving VSs to a different datastore

    Posted Feb 17, 2016 11:47 AM

    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



  • 2.  RE: Moving VSs to a different datastore
    Best Answer

    Posted Feb 17, 2016 12:13 PM

    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)"

    }



  • 3.  RE: Moving VSs to a different datastore

    Posted Feb 17, 2016 01:53 PM

    Many thanks!

    marius