VMware Cloud Community
rjmend
Contributor
Contributor

Script for Storage vMotion from one datastore to another - 10 at a time

Hello,

Ive done some searching and could not find what I'm looking for so I am making a post: I would like to storage vMotion from one datastore to another, 10 at a time, and output the vm's migrated to a log file. I have this script so far that I have edited. Please let me know how to add the instructions on how to append to a log file and also 10 at a time. the -RunAsync also will not let me migrate one at a time so I may have to remove that as well. Any help?

# My Login Credentials

$vi_server = "X.X.X.X" # vCenter IP

$vcuser = "MyLogin@vsphere.local" # My Username

$vcpass = "VerySecurePassword123" # My Password

# Connect to vCenter

Connect-VIServer -Server $vi_server -User $vcuser -Password $vcpass

# Old datastore and new datastore

$OldDatastores = Get-Datastore TEST-01

$NewDatastores = Get-Datastore TEST-02

$i = 0

# Get all VMs in each old datastore and move them

Foreach ($OldDatastore in $OldDatastores){

    $VMs = Get-VM -Datastore $OldDatastore

    Foreach ($VM in $VMs)

    {

# Move the VM to a new datastore

$VM | Move-VM -Datastore $NewDatastores[$i] -RunAsync

    }

    $i++

    # Wait timer for next migrations

    Start-Sleep 5

}

0 Kudos
2 Replies
Gidrakos
Hot Shot
Hot Shot

For simple logging, use Out-File whenever you'd like something to be written to a file.

"Stuff I want written to Log." | Out-File [Path_to_file].txt -Append

For doing them in batches of 10, you could always implement tasks which, combined with -runasync, would initiate a set number of Move-VM instances and wait for them to complete before moving on to the next group. You can see a good example of both -runasync and tasks here: PowerCLI's RunAsync Parameter Rocks!

You'd end up with something like:

$ii = 0

$tasks = @

for ($Vm in $VMs) {

       # No dividing by 0!

       if ($ii -eq 0 -or ( 10 / $ii ) -ne 1 ) {

           $tasks += $VM | Move-VM -Datastore $NewDatastores[$i] -RunAsync

       }

     else {

         # We have a group of 10, let's start it!

         $tasks | wait-task

         # After we finish waiting, start over, but don't forget to add the current VM first.

         $tasks = @

         $tasks += $VM | Move-VM -Datastore $NewDatastores[$i] -RunAsync

     }

     $ii++

}

0 Kudos
Scotslad007
Contributor
Contributor

0 Kudos