VMware Cloud Community
matt5stow
Contributor
Contributor

Schedule task for compute / storage vMotion - CLI

I am trying to find a script so I can create scheduled tasks via CLI so I can migrate multiple VMs as once to new hosts and storage. 

Scheduling one server at a time via the vCenter GUI works but would like to create multiple scheduled as we have hundreds of servers to migrate. 

I have seen post about scheduling snapshots via CLI but not migrations. 

Please help!

Labels (4)
0 Kudos
22 Replies
fabio1975
Commander
Commander

Ciao

In the past I have used this

https://ranchh.wordpress.com/2018/02/16/powershell-script-vmware-storage-vmotion/

is based on an input csv file with VM name and target storage.

Export the list of all VMs in csv file and add the datastore field to it.

Fabio

Visit vmvirtual.blog
If you're satisfied give me a kudos

0 Kudos
LucD
Leadership
Leadership

If you actually want to do the vMotions via Scheduled Tasks, have a look at my Scheduled Tasks – MethodAction post.


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

0 Kudos
matt5stow
Contributor
Contributor

Thanks for the reply. I took a look and to be honest my scripting skills are not great so was hoping for a bit more guidance if at all possible...

0 Kudos
LucD
Leadership
Leadership

Which script did you look at?
And what kind of guidance do you need?
Are the VMs to be moved available in a file? Format? Content?


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

0 Kudos
matt5stow
Contributor
Contributor

Hi,

 

I looked at your script and understand some part but am new to scripting. The machines to migrate would ideally be in a CSV file that we can edit as we will be running migrations early throughout the month. 

So what I would like to achieve is a script to create a scheduled task(s) within the VC to do the vMotions and Storage vMotion. Basically automate the creation of the scheduled task. 

Other option I am exploring is a script to migrate the servers and setting it up in a Windows scheduled task to run.

Any thoughts / assistance / advice would be much appreciated! 

0 Kudos
LucD
Leadership
Leadership

You can start from a CSV that contains all the required info.
An example is in the code below.

Be aware that choosing the same Time for all vMotion, might stress your vCenter.
And the vCenter in any case will only actively run X-number of the tasks in parallel.

# CSV layout
#
# VM,VMHost,Datastore,Time
# VM1,esx1.domain,datastore1,22/04/2021 19:00
# VM2,esx2.domain,datastore1,22/04/2021 19:01


$csvName = '.\vmotion.csv'

$emailAddr = "lucd@lucd.info"

$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

Import-Csv -Path $csvName -PipelineVariable row  | ForEach-Object {
    $vm = Get-View -ViewType VirtualMachine -Filter @{Name = "^$($row.VM)$" }
    $tgtDatastore = $row.Datastore
    $vmHost = Get-View -ViewType HostSystem -Filter @{Name = "$($row.VMHost)"}
    $startTime = [DateTime]::ParseExact($row.Time,'dd/MM/yyyy HH:mm',$null)

    $spec = New-Object VMware.Vim.ScheduledTaskSpec
    $spec.Name = "svMotion " + $row.VM
    $spec.Description = "Migrate " + $_.VMname + " to " + $tgtDatastore
    $spec.Enabled = $true
    $spec.Notification = $emailAddr
    $spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
    $spec.Scheduler.runat = $startTime

    $spec.Action = New-Object VMware.Vim.MethodAction
    $spec.Action.Name = "RelocateVM_Task"
    $arg1 = New-Object VMware.Vim.MethodActionArgument
    $arg1.Value = New-Object VMware.Vim.VirtualMachineRelocateSpec
    $arg1.Value.datastore = (Get-Datastore $tgtDatastore | Get-View).MoRef
    $arg1.Value.pool = $vm.ResourcePool
    $arg1.Value.host = $vmHost.MoRef
    $spec.Action.Argument += $arg1
    $arg2 = New-Object VMware.Vim.MethodActionArgument
    $arg2.Value = [VMware.Vim.VirtualMachineMovePriority]"defaultPriority"
    $spec.Action.Argument += $arg2

    $scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)
}

 


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

matt5stow
Contributor
Contributor

Thank you so much, it is much appriciated! Am I correct in thinking that once I run it then a scheduled task should appear for each VM specified in the CSV correct? Just tried it and can`t see anything.

0 Kudos
LucD
Leadership
Leadership

You have to select the vCenter, and then Configure - Scheduled Tasks


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

0 Kudos
matt5stow
Contributor
Contributor

I ran the code as a PS1, making suer I`m connected to the vCenter. It doesn`t produce any output and can`t see anything appear in the vCenter at all under the scheduled tasks,

Tags (1)
0 Kudos
LucD
Leadership
Leadership

That should be under Configure - Scheduled Tasks not Monitor.

I just tested it again with a CSV that has 2 VMs, and for me it seems to work.


scheduled task.png


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

0 Kudos
matt5stow
Contributor
Contributor

It would help if I put the header rows in the CSV! Sorry and thank you so much, all working perfectly. I`ll take the script away and do some learning! 

0 Kudos
LucD
Leadership
Leadership

😀


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

0 Kudos
matt5stow
Contributor
Contributor

.

0 Kudos
matt5stow
Contributor
Contributor

Hi – so when I run the task it returns an error.

 

A specified parameter was not correct: spec.pool

 

If I edit the task it isn`t selecting the “Select vMotion priority” option and neither radio buttons are checked.

 

If I select one and run through the task and then run it manually then it works.

 

Any ideas?!

 

Thanks

0 Kudos
LucD
Leadership
Leadership

Are you connected to the vCenter or an ESXi node?
Is that for a specific VM?
What does this return for those VM(s)?

Get-VM | Select Name,@{N='ResourcePool';E={(Get-View -Id $_.ExtensionData.ResourcePool).Name}}

Are you moving VMs to another cluster with different resourcepools?


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

0 Kudos
matt5stow
Contributor
Contributor

Connecting to the vCenter. It seems to be for any VM I try with and tried the same on two different vCenters. 

I ran the code and returned the list of VMs in the vCenter - with resource Pool as "resource". That`s the same for all the VMs.

And yes I am trying to hot migrate the VMs from one cluster to another, on different storage. When I do a manual vMotion and storage vMotion is works fine.

So to clarify:

The schedule task is created but fails to run with the error.

Soon as I edit the schedule task but don`t change any settings it then works great. 

0 Kudos
LucD
Leadership
Leadership

Try replacing the Pool line with

$arg1.Value.pool = (Get-View -Id (Get-View -Id $vmHost.Parent).ResourcePool).MoRef


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

matt5stow
Contributor
Contributor

Working perfectly thank you! 🙂 

0 Kudos
matt5stow
Contributor
Contributor

So here in an interesting one. When I do a manual migrate on storage only I can see the datastore clusters no problem. When I choose "change both compute resource and storage" I can only see datastores and no clusters? 

I was going to see if I can point the scheduled task to the datastore cluster instead of a specific datastore and let DRS take care of it so any specific datastore doesn`t run the risk of filling up during migrations. 

Even the GUI says you should be able to select a cluster:

"Migrate the virtual machines to a specific host or cluster and their storage to a specific datastore or datastore cluster."

Any ideas or is it a limitation?

0 Kudos