Home > PowerCLI, PowerShell, Scheduled Task, vSphere > Scheduled Tasks – MethodAction

Scheduled Tasks – MethodAction

October 18th, 2009 LucD Leave a comment Go to comments

With the introduction of vSPhere the types of Tasks you can select when you create a new Scheduled Task has increased. This is a very useful feature that allows you to schedule for example your (s)vMotions, your Snapshots, your Imports and Exports and so on.

Schedule-Task-Listbox

In the PowerCLI Community there was a recent question on how these Scheduled Tasks can be created from PowerShell (see relocate vm’s from csv file and create schedule task in VC).

Being able to create a Scheduled Task for a svMotion for several guests from a PowerShell script, instead of clicking away in the vSphere Client, would be another step on the path of vSphere automation.

The current PowerCLI 4 (build 162509) unfortunately has no cmdlets for Scheduled Tasks. But the SDK contains the CreateScheduledTask method that can be used for this purpose.

The key parameter to this method is the ScheduledTaskSpec object. In the action property of this object you specify which type of action you want the scheduled task to take. If we want to schedule a Task, we will have to select the MethodAction extension object.

But then it looks as if we’re stuck. The SDK Reference doesn’t say which are the accepted values for the name property in the MethodAction object.

Luckily, with a bit of reverse engineering and with the help of Fiddler (see my The Onyx alternative ? post), I was able to get the accepted values for the name property.

In fact the set up is quite straightforward, the name property holds the name of the SDK method that performs the type of task requested. And the argument property, which is an array, will hold all the parameters that go with the called method.

An example.

We want to “Migrate a virtual machine” and move the VM to another datastore.

This is a svMotion.

That means we need the RelocateVM_Task method.

This method requires 2 parameters.

RelocateVM_Task

We will have to pass those parameters for the method in the argument array in the MethodAction object. Schematically, the MethodAction object will look like this

MethodAction-Properties

MethodAction name values

The following table shows which SDK method corresponds with which Task. With the name of the SDK method you can find the parameters that need to go in the MethodAction.argument property.

Task MethodAction.name
Add a host AddStandaloneHost_Task
Change resource settings of Resource Pool or Virtual Machine UpdateConfig
Change the power state of a virtual machine ResetVM_Task
Check compliance for a profile CheckProfileCompliance_Task
Clone a virtual machine CloneVM_Task
Create a virtual machine CreateVM_Task
Deploy a virtual machine CloneVM_Task
Make a snapshot of a virtual machine CreateSnapshot_Task
Migrate a virtual machine RelocateVM_Task

Note1: The import and export a virtual machine tasks do not use the MethodAction. They use CreateTaskAction and the TaskTypeId is respectively com.vmware.converter.Import and com.vmware.converter.Export

Note2: The Scan and Remediate tasks also use CreateTaskAction. The TaskTypeId is respectively com.vmware.vcIntegrity.ScanTask and com.vmware.vcIntegrity.RemediateTask

The script

The Virtual Machine names come from a CSV file with 1 column and with the header VMname.

The script looks like this:

$csvName = "C:\Test.csv"
$tgtDatastore = "Datastore1"
$emailAddr = "lucd@lucd.info"
$startTime = [Datetime]"10/21/2009 20:00"
$startInterval = 5

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

$offset = 0
Import-Csv $csvName | % {
$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)
}
Task