<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scheduled Task Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/category/vsphere/scheduled-task/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/category/vsphere/scheduled-task/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Thu, 03 Feb 2011 23:49:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.10</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>Scheduled Task Archives - LucD notes</title>
	<link>https://www.lucd.info/category/vsphere/scheduled-task/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Scheduled Tasks &#8211; MethodAction</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/</link>
					<comments>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sun, 18 Oct 2009 01:31:32 +0000</pubDate>
				<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scheduled Task]]></category>
		<category><![CDATA[vSphere]]></category>
		<category><![CDATA[SDK]]></category>
		<guid isPermaLink="false">http://lucd.info/?p=689</guid>

					<description><![CDATA[With the introduction of vSPhere the types of Tasks you can select when [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>With the introduction of vSPhere the types of Tasks you can select when you create a new <strong>Scheduled Task</strong> 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.</p>
<p><a rel="attachment wp-att-691" href="https://lucd.info/2009/10/18/scheduled-tasks-methodaction/schedule-task-listbox/"><img fetchpriority="high" decoding="async" class="aligncenter size-full wp-image-691" title="Schedule-Task-Listbox" src="https://lucd.info/wp-content/uploads/2009/10/Schedule-Task-Listbox.png" alt="Schedule-Task-Listbox" width="340" height="181" srcset="https://www.lucd.info/wp-content/uploads/2009/10/Schedule-Task-Listbox.png 340w, https://www.lucd.info/wp-content/uploads/2009/10/Schedule-Task-Listbox-300x159.png 300w" sizes="(max-width: 340px) 100vw, 340px" /></a></p>
<p>In the <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/windows_toolkit?view=discussions&amp;numResults=15" target="_blank">PowerCLI Community</a> there was a recent question on how these Scheduled Tasks can be created from PowerShell (see <a href="https://communities.vmware.com/thread/236684?tstart=0" target="_blank">relocate vm&#8217;s from csv file and create schedule task in VC</a>).</p>
<p>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.<span id="more-689"></span></p>
<p>The current <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/windows_toolkit" target="_blank">PowerCLI</a> 4 (build 162509) unfortunately has no cmdlets for Scheduled Tasks. But the SDK contains the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.scheduler.ScheduledTaskManager.html#create" target="_blank">CreateScheduledTask</a> method that can be used for this purpose.</p>
<p>The key parameter to this method is the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.scheduler.ScheduledTaskSpec.html" target="_blank">ScheduledTaskSpec</a> object. In the <strong>action</strong> 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 <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.action.MethodAction.html" target="_blank">MethodAction</a> extension object.</p>
<p>But then it looks as if we&#8217;re stuck. The SDK Reference doesn&#8217;t say which are the accepted values for the <strong>name</strong> property in the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.action.MethodAction.html" target="_blank">MethodAction</a> object.</p>
<p>Luckily, with a bit of reverse engineering and with the help of Fiddler (see my <a href="https://lucd.info/2009/09/26/the-onyx-alternative/" target="_blank">The Onyx alternative ?</a> post), I was able to get the accepted values for the <strong>name</strong> property.</p>
<p>In fact the set up is quite straightforward, the <strong>name</strong> property holds the name of the SDK method that performs the type of task requested. And the <strong>argument</strong> property, which is an array, will hold all the parameters that go with the called method.</p>
<h2><strong>An example</strong>.</h2>
<p>We want to &#8220;Migrate a virtual machine&#8221; and move the VM to another datastore.</p>
<p>This is a <strong>svMotion</strong>.</p>
<p>That means we need the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html#relocate" target="_blank">RelocateVM_Task</a> method.</p>
<p style="text-align: left;">This method requires 2 parameters.</p>
<p style="text-align: left;"><a rel="attachment wp-att-706" href="https://lucd.info/2009/10/18/scheduled-tasks-methodaction/relocatevm_task/"><img decoding="async" class="size-full wp-image-706 aligncenter" title="RelocateVM_Task" src="https://lucd.info/wp-content/uploads/2009/10/RelocateVM_Task.png" alt="RelocateVM_Task" width="646" height="133" srcset="https://www.lucd.info/wp-content/uploads/2009/10/RelocateVM_Task.png 808w, https://www.lucd.info/wp-content/uploads/2009/10/RelocateVM_Task-300x61.png 300w" sizes="(max-width: 646px) 100vw, 646px" /></a></p>
<p>We will have to pass those parameters for the method in the <strong>argument</strong> array in the <strong>MethodAction</strong> object. Schematically, the <strong>MethodAction</strong> object will look like this</p>
<p style="text-align: left;"><a rel="attachment wp-att-720" href="https://lucd.info/2009/10/18/scheduled-tasks-methodaction/methodaction-properties-2/"><img decoding="async" class="aligncenter size-full wp-image-720" title="MethodAction-Properties" src="https://lucd.info/wp-content/uploads/2009/10/MethodAction-Properties1.png" alt="MethodAction-Properties" width="494" height="177" srcset="https://www.lucd.info/wp-content/uploads/2009/10/MethodAction-Properties1.png 494w, https://www.lucd.info/wp-content/uploads/2009/10/MethodAction-Properties1-300x107.png 300w" sizes="(max-width: 494px) 100vw, 494px" /></a></p>
<h2>MethodAction name values</h2>
<p>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.</p>
<table style="width: 572px; height: 303px;" border="0" frame="border" rules="all">
<tbody>
<tr>
<td style="text-align: center;"><strong>Task</strong></td>
<td style="text-align: center;"><strong>MethodAction.name</strong></td>
</tr>
<tr>
<td>Add a host</td>
<td>AddStandaloneHost_Task</td>
</tr>
<tr>
<td>Change resource settings of Resource Pool or Virtual Machine</td>
<td>UpdateConfig</td>
</tr>
<tr>
<td>Change the power state of a virtual machine</td>
<td>ResetVM_Task</td>
</tr>
<tr>
<td>Check compliance for a profile</td>
<td>CheckProfileCompliance_Task</td>
</tr>
<tr>
<td>Clone a virtual machine</td>
<td>CloneVM_Task</td>
</tr>
<tr>
<td>Create a virtual machine</td>
<td>CreateVM_Task</td>
</tr>
<tr>
<td>Deploy a virtual machine</td>
<td>CloneVM_Task</td>
</tr>
<tr>
<td>Make a snapshot of a virtual machine</td>
<td>CreateSnapshot_Task</td>
</tr>
<tr>
<td>Migrate a virtual machine</td>
<td>RelocateVM_Task</td>
</tr>
</tbody>
</table>
<p><span style="text-decoration: underline;">Note1</span>: The import and export a virtual machine tasks do not use the MethodAction. They use <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.action.CreateTaskAction.html" target="_blank">CreateTaskAction</a> and the <strong>TaskTypeId</strong> is respectively <strong>com.vmware.converter.Import</strong> and <strong>com.vmware.converter.Export</strong></p>
<p><span style="text-decoration: underline;">Note2</span>: The Scan and Remediate tasks also use <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.action.CreateTaskAction.html" target="_blank">CreateTaskAction</a>. The <strong>TaskTypeId</strong> is respectively <strong>com.vmware.vcIntegrity.ScanTask</strong> and <strong>com.vmware.vcIntegrity.RemediateTask</strong></p>
<h2>The script</h2>
<p>The Virtual Machine names come from a <strong>CSV</strong> file with <strong>1 column</strong> and with the header <strong>VMname</strong>.</p>
<p>The script looks like this:</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$csvName = &quot;C:\Test.csv&quot;
$tgtDatastore = &quot;Datastore1&quot;
$emailAddr = &quot;lucd@lucd.info&quot;
$startTime = [Datetime]&quot;10/21/2009 20:00&quot;
$startInterval = 5

$si = get-view ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

$offset = 0
Import-Csv $csvName | % {
$vm = Get-View -ViewType VirtualMachine -Filter @{&quot;Name&quot;=$_.VMname}

$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = &quot;svMotion &quot; + $_.VMname
$spec.Description = &quot;Migrate &quot; + $_.VMname + &quot; to &quot; + $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 = &quot;RelocateVM_Task&quot;

$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]&quot;defaultPriority&quot;
$spec.Action.Argument += $arg2

$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)
}</pre><p></p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 776px; width: 1px; height: 1px;">
<table style="border-collapse: collapse; width: 397px;" border="0" cellspacing="0" cellpadding="0">
<col style="width: 298pt;" width="397"></col>
<tbody>
<tr style="height: 15pt;" height="20">
<td class="xl65" style="height: 15pt; width: 298pt;" width="397" height="20">Task</td>
</tr>
</tbody>
</table>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/feed/</wfw:commentRss>
			<slash:comments>19</slash:comments>
		
		
			</item>
	</channel>
</rss>
