I created a script to create scheduled tasks to move VMs among datastores based on the excellent page Scheduled Tasks - MethodAction - LucD notes
It worked fine until I replaced the old vCenter 5.5 with a VCSA 6.5.
I attempted to use the same script but I got lots of errors and I cannot see the list of created scheduled tasks.
Should I change the script?
Regards
marius
Just tried the script, slightly adapted, on vSphere 6.7.
Seems to work for.
From the error you listed, it might be that there is a MoRef missing.
Can you try with this variation (ther eis no CSV required, I place the VM name in a here-string and use ConvertFrom-Csv.
Make sure that you pick a name for an existing VM.
Check that the Get-View actually returns 1 VM object.
Update the $tgtDatastore with a name of one of your datastores.
VMname
TS2
'@
$tgtDatastore = "DS2"
$emailAddr = "lucd@lucd.info"
$startTime = (Get-Date).AddDays(2)
$startInterval = 5
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$offset = 0
ConvertFrom-Csv -InputObject $csv | % {
$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 VMware.Vim.VirtualMachineRelocateSpec
$arg1.Value.datastore = (Get-Datastore $tgtDatastore | Get-View).MoRef
$arg1.Value.pool = $vm.ResourcePool
$arg1.Value.host = $vm.Runtime.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)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you attach the script?
And show the first errors you are getting?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The errors look like:
Eccezione durante la chiamata di "CreateScheduledTask" con "2" argomento/i: "
Required parameter entity is missing
while parsing call information for method CreateScheduledTask
at line 1, column 171
while parsing SOAP body
at line 1, column 64
while parsing SOAP envelope
at line 1, column 0
while parsing HTTP request for method create
on object of type vim.scheduler.ScheduledTaskManager
at line 1, column 0"
In C:\Users\<my_script>.ps1:64 car:1
+ $scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
The first line (in Italian) sounds like:
Exception while calling "CreateScheduledTask" with "2" argomento/i: "
The script is based on yours, with some minimal customization.
Regards
marius
Just tried the script, slightly adapted, on vSphere 6.7.
Seems to work for.
From the error you listed, it might be that there is a MoRef missing.
Can you try with this variation (ther eis no CSV required, I place the VM name in a here-string and use ConvertFrom-Csv.
Make sure that you pick a name for an existing VM.
Check that the Get-View actually returns 1 VM object.
Update the $tgtDatastore with a name of one of your datastores.
VMname
TS2
'@
$tgtDatastore = "DS2"
$emailAddr = "lucd@lucd.info"
$startTime = (Get-Date).AddDays(2)
$startInterval = 5
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$offset = 0
ConvertFrom-Csv -InputObject $csv | % {
$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 VMware.Vim.VirtualMachineRelocateSpec
$arg1.Value.datastore = (Get-Datastore $tgtDatastore | Get-View).MoRef
$arg1.Value.pool = $vm.ResourcePool
$arg1.Value.host = $vm.Runtime.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)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Many thanks.
Let me provde a feed-back, I hope it will useful for other people.
The original script based on the original sample provided by LucD worked fine without changes.
The error was due to an unwanted space in the name of the VM used for testing.
Many thanks again
marius
I made some further tests and discovered that the scheduled task is created fine.
However, the scheduled task is set to change either host and datastore, so that if the VM is powered on I get an error saying that the task cannot be completed in the current state of the VM.
How can I specify that the task must change only the datastore, so that the operation can be completed even if the VM is powered on?
Regards
marius
Try by just leaving out the Host and Pool assignments.
Something like this
VMname
TS2
'@
$tgtDatastore = "DS2"
$emailAddr = "lucd@lucd.info"
$startTime = (Get-Date).AddDays(2)
$startInterval = 5
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$offset = 0
ConvertFrom-Csv -InputObject $csv | % {
$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 VMware.Vim.VirtualMachineRelocateSpec
$arg1.Value.datastore = (Get-Datastore $tgtDatastore | Get-View).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