0

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" # Set to your vCenter hostname|IP

$vcuser = "MyLogin@vsphere.local" # Set to your vCenter username to connect

$vcpass = "VerySecurePassword123" # Set to your vCenter username password to connect



# Connect to vCenter

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


# I want all old and new datastores as objects in arrays

$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

    }

2

To get batches of 10 you can use Select-Object with the parameters -First and -Skip:

$start = 0
do {
    $VMs |Select -First 10 -Skip $start | 
        Move-VM -Datastore $NewDatastore |
        Select Name |Out-File -FilePath "c:\logfile" -Append
    $start += 10
}
until ($start -gt $VMs.Length)

Appending to a file is just a parameter for Out-File.

Using -RunAsync is counterproductive, since it causes the cmdlet to run in the background. So it would just start one batch after another and all run simultaneously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.