I try to run a script to create scheduled tasks to move VMs from one datastore to a different one.
I use the VMware.Vim.VirtualMachineRelocateSpec object to specify where each VM must be relocated.
I want to change only the deatastore, not the host, but while creating the scheduled tast I cannot specify "Change datastore" because "Change both host and datastore" is selected anyway.
This is a problem if the VM is powered on...
Can anybody please help?
Regards
marius
As per official API documentation@ VMware.Vim.VirtualMachineRelocateSpec ,
We can just specify target datastore, hence any vSphere SDK + PowerCLI also must honor it.
Can you please post your script here?
As per official API documentation@ VMware.Vim.VirtualMachineRelocateSpec ,
We can just specify target datastore, hence any vSphere SDK + PowerCLI also must honor it.
Can you please post your script here?
Per the documentation, you don't have to specify a host when setting up the specification. Thus, leaving the host property unset should work as a valid relocation spec.
In include my script, based on what suggested by LucD while answering to my prevuous post Creating scheduled tasks
#
# Based on da http://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/
#
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer <IP Address> -User <username>
$csvName = "E:\Test.csv"
$tgtDatastore = <target datastore name>
$emailAddr = <my e-mail address>
$startTime = [Datetime]"10/21/2016 20:00" # late in the future, just to see if the script works
$startInterval = 5
$si = get-view ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$offset = 0
Import-Csv $csvName | % {
$a = $_.VMname
$a
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name"=$_.VMname}
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "svMotion " + $_.VMname
$spec.Description = "Migrate " + $_.VMname + " to " + $tgtDatastore
$spec.Enabled = $true
$spec.Notification = $emailAddr
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
$spec.Scheduler.runat = $startTime.AddMinutes($offset)
$offset += $startInterval
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "RelocateVM_Task"
$arg1 = New-Object VMware.Vim.MethodActionArgument
$arg1.Value = New-Object
$arg1.Value.datastore = (Get-Datastore $tgtDatastore | Get-View).MoRef
$arg1.Value.pool = $vm.ResourcePool
$arg1.Value.host = $vm.Runtime.Host
$arg1.Value.host
$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)
}
The scheduled task creation is fine, but the task is based on changing both host and datastore.
How can I change the settings?
Regards
marius
I made some further tests and I discovered that:
Is there any way to specify that I want only to change the datastore because my VM is powered on?
Regards
marius
I made a further test in a lab environment and I noticed that the tasks created with the script work fine, even if they look to be set in the wrong way.
I apologize for dubiting...
Many thanks to LucD.
Regards
marius