VMware Cloud Community
justinsmith
Enthusiast
Enthusiast
Jump to solution

PowerCLI script to svMotion VM's based on a list of VM's

Im looking to svMotion VM's based on a spreadsheet or txt file (either will work for me) from one datastore to another. I've found simple ones that do it, but nothing that will import a csv file or txt file and svmotion those VM's.

Reply
0 Kudos
46 Replies
LucD
Leadership
Leadership
Jump to solution

It might be that the Where-clause in the function is not catching the running vMotions.

When they are running, what does this return ?

(Get-Task | Where{ ($_.PercentComplete -ne 100) -and ( ($_.Description -like '*DRS*') -or ($_.Description -like '*vMotion*') )} | Measure-Object).Count


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

It returns 0

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is the reason why the function doesn't work.

The object returned by Get-Task has changed over the different PowerCLI builds.

If you do a (again while there are some vMotion running)

Get-Task | Select -First 1 -Property *

what is returned ?


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

ServerId    : /VIServer=domain\user@vcentername:443/
State       : Running
IsCancelable: True

PercentComplete : 5

StartTime   : 10/23/2014 11:42:51 AM
FinishTime  :
ObjectId    : VirtualMachine-vm-99187
Result      :
Description : Relocate virtual machine

ExtensionData   : VMware.Vim.Task

Id          : Task-task-1059314
Name        : RelocateVM_Task
Uid         : /VIServer=domain\user@vcentername:443/Task=Task-task-1059314/
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing line 37 (in the original function) into this

$NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Relocate')} | Measure-Object).Count


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

What about like 39? Change it to the same thing?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, of course I forgot.

Change the Where-clause on line 42 as well


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

My line #'s were off due to adding some other commands, but I picked up what you were laying down. It works like a champ Thanks LucD, ZSoldier, and brentcochran1 for the help.

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

To get this to go to a Storage Datacluster I changed:

$ds = get-datastore $row.destds

To:

$ds = get-datastorecluster $row.destds

It doesnt seem to be queuing up the storage vMotions now, where as it did when I pointed it directly at the Datastore and not a Datastore cluster. Make sense?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean the svMotions are now executed in sequence, not in parallel ?

Or is none of the svMotions taking place ?

In the latter case, could it be that SDRS made some recommendations, and is now waiting for which rule you want to approve ?


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

When I ran it with just the individual datastore and not the datastore cluster, it will svmotion no more than 4 at a time. When ran against the datastore cluster the all start sVMotioning at the same time.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could the different behavior be explained by the fact that the svMotions are going to different datastores ?

While before you had a higher number of svMotions going to the same datastore.

See Limits on Simultaneous Migrations in the vSphere Web Client


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

Ah, potentially.

I just know it ran 25 svMotions at the same time. Is there a way to queue up a number even though its going to a datastore cluster?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could also be that the $NumvMotionTasks value is not correct.

Put some Write-Verbose lines in the script to display the value each time it enters the loop.

Should give you an idea what value the script calculates


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

Here's whats in the script:

$NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Relocate')} | Measure-Object).Count

}

Write-Verbose "$(Get-Date)- Proceeding."

The output when I run it shows what it typically would when migrating a VM via script.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could you perhaps attach the script you are currently using to the thread.

There have been several updates, so I don't know exactly what you are running anymore.


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

Connect-VIServer vsphereqa

function Wait-mTaskvMotions {

[CmdletBinding()]

Param(

  [int] $vMotionLimit=1,

  [int] $DelayMinutes=5

)

$NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Relocate')} | Measure-Object).Count

While ( $NumvMotionTasks -ge $vMotionLimit ) {

  Write-Verbose "$(Get-Date)- Waiting $($DelayMinutes) minute(s) before checking again."

  Start-Sleep ($DelayMinutes * 60)

$NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Relocate')} | Measure-Object).Count

}

Write-Verbose "$(Get-Date)- Proceeding."

} # end function

$filepath = "H:\VM Scripts\test_svmotion.csv"

$csvobj = import-csv $filepath

foreach ($row in $csvobj) {

     $vmobj = get-vm $row.vmname

     $ds = get-datastorecluster $row.destds

     $vmobj | move-vm -datastore $ds -confirm:$false -runasync

     Wait-mTaskvMotions -vMotionLimit 4 # This will keep going through the foreach loop until 4 tasks are registered (vMotion or Storage vMotion), waits 5 minutes between checks.  Will only continue to process loop when vMotion tasks are less than 4.

     }

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I guess that you run this against a vSphere 5.* environment ?

If yes, the Description property has a different content.

Try like this, I changed the target on the -match operator.

If this doesn't work, check what is actually in the Description property for a vMotion and a svMotion task

Connect-VIServer vsphereqa

function Wait-mTaskvMotions {

    [CmdletBinding()]

    Param(

      [int] $vMotionLimit=1,

      [int] $DelayMinutes=5

    )

    $NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Apply Storage DRS recommendations|Migrate virtual machine')} | Measure-Object).Count

    While ( $NumvMotionTasks -ge $vMotionLimit ) {

        Write-Verbose "$(Get-Date)- Waiting $($DelayMinutes) minute(s) before checking again."

        Start-Sleep ($DelayMinutes * 60)

        $NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ($_.Description -match 'Apply Storage DRS recommendations|Migrate virtual machine')} | Measure-Object).Count

    }

   

    Write-Verbose "$(Get-Date)- Proceeding."

} # end function

$filepath = "H:\VM Scripts\test_svmotion.csv"

$csvobj = import-csv $filepath

foreach ($row in $csvobj) {

     $vmobj = get-vm $row.vmname

     $ds = get-datastorecluster $row.destds

     $vmobj | move-vm -datastore $ds -confirm:$false -runasync

     Wait-mTaskvMotions -vMotionLimit 4 # This will keep going through the foreach loop until 4 tasks are registered (vMotion or Storage vMotion), waits 5 minutes between checks.  Will only continue to process loop when vMotion tasks are less than 4.

}


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

Reply
0 Kudos
MandeepJ
Enthusiast
Enthusiast
Jump to solution

Luc, i want to capture svmotion start and end time or creating a separate log file is also fine. Any idea how to do that?

I will add that to the Brent's script.

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect you would need to fetch the task objects for that.

Something like I did in Events – Part 8 – vMotion history


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

Reply
0 Kudos