<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: Scheduled Tasks &#8211; MethodAction	</title>
	<atom:link href="https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Fri, 01 May 2020 01:21:05 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.9</generator>
<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>
		By: simbol		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4842</link>

		<dc:creator><![CDATA[simbol]]></dc:creator>
		<pubDate>Fri, 01 May 2020 01:21:05 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-4842</guid>

					<description><![CDATA[Hi all,

I`m struggling with my Script to schedule a Snapshot-remove. Is this even possible via script? In the other hand, create a scheduled snapshot create task works fine.

thanks a lot


$vm = Get-VM -Name &quot;test&quot;
$si = get-view ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
# Validate datetime value and convert to UTC
try { $castRunTime = ([datetime]$runTime).ToUniversalTime() } catch { &quot;Unable to convert runtime parameter to date time value&quot;; break }
if ( [datetime]$runTime -lt (Get-Date) ) { &quot;Single run tasks can not be scheduled to run in the past.  Please adjust start time and try again.&quot;; break }
$spec.Scheduler.RunAt = $castRunTime
$spec.Name = $snapName
$spec.Enabled = $true
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = &quot;RemoveSnapshot_Task&quot;
@($snapName) &#124; %{

    $arg = New-Object VMware.Vim.MethodActionArgument

    $arg.Value = $_

    $spec.Action.Argument += $arg

}

$scheduledTaskManager.CreateObjectScheduledTask($vm.MoRef,$spec)

----------------------------

ESXi Version 6.5

Error Output:

Exception calling &quot;CreateObjectScheduledTask&quot; with &quot;2&quot; argument(s): &quot;
Required parameter obj is missing
while parsing call information for method CreateObjectScheduledTask
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 createObjectScheduledTask
on object of type vim.scheduler.ScheduledTaskManager
at line 1, column 0&quot;
At line:1 char:1
+ $scheduledTaskManager.CreateObjectScheduledTask($vm.MoRef, $spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : VimException]]></description>
			<content:encoded><![CDATA[<p>Hi all,</p>
<p>I`m struggling with my Script to schedule a Snapshot-remove. Is this even possible via script? In the other hand, create a scheduled snapshot create task works fine.</p>
<p>thanks a lot</p>
<p>$vm = Get-VM -Name &#8220;test&#8221;<br />
$si = get-view ServiceInstance<br />
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager<br />
$spec = New-Object VMware.Vim.ScheduledTaskSpec<br />
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler<br />
# Validate datetime value and convert to UTC<br />
try { $castRunTime = ([datetime]$runTime).ToUniversalTime() } catch { &#8220;Unable to convert runtime parameter to date time value&#8221;; break }<br />
if ( [datetime]$runTime -lt (Get-Date) ) { &#8220;Single run tasks can not be scheduled to run in the past.  Please adjust start time and try again.&#8221;; break }<br />
$spec.Scheduler.RunAt = $castRunTime<br />
$spec.Name = $snapName<br />
$spec.Enabled = $true<br />
$spec.Action = New-Object VMware.Vim.MethodAction<br />
$spec.Action.Name = &#8220;RemoveSnapshot_Task&#8221;<br />
@($snapName) | %{</p>
<p>    $arg = New-Object VMware.Vim.MethodActionArgument</p>
<p>    $arg.Value = $_</p>
<p>    $spec.Action.Argument += $arg</p>
<p>}</p>
<p>$scheduledTaskManager.CreateObjectScheduledTask($vm.MoRef,$spec)</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>ESXi Version 6.5</p>
<p>Error Output:</p>
<p>Exception calling &#8220;CreateObjectScheduledTask&#8221; with &#8220;2&#8221; argument(s): &#8221;<br />
Required parameter obj is missing<br />
while parsing call information for method CreateObjectScheduledTask<br />
at line 1, column 171<br />
while parsing SOAP body<br />
at line 1, column 64<br />
while parsing SOAP envelope<br />
at line 1, column 0<br />
while parsing HTTP request for method createObjectScheduledTask<br />
on object of type vim.scheduler.ScheduledTaskManager<br />
at line 1, column 0&#8243;<br />
At line:1 char:1<br />
+ $scheduledTaskManager.CreateObjectScheduledTask($vm.MoRef, $spec)<br />
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br />
    + FullyQualifiedErrorId : VimException</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: LucD		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4677</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Tue, 06 Aug 2019 09:14:38 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-4677</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4673&quot;&gt;marius&lt;/a&gt;.

Hi Marius,
In theory that is possible, you can define the complete VirtualMachineRelocateSpec object (1st argument) in the Scheduled Task.
But since converting the disk type needs to be done per disk, you will need to target VMs with the same number of VMDK and the DeviceId, which you also need to specify, needs to be the same on all VMs.]]></description>
			<content:encoded><![CDATA[<p>Hi Marius,<br />
In theory that is possible, you can define the complete VirtualMachineRelocateSpec object (1st argument) in the Scheduled Task.<br />
But since converting the disk type needs to be done per disk, you will need to target VMs with the same number of VMDK and the DeviceId, which you also need to specify, needs to be the same on all VMs.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: marius		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4673</link>

		<dc:creator><![CDATA[marius]]></dc:creator>
		<pubDate>Tue, 23 Jul 2019 11:14:58 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-4673</guid>

					<description><![CDATA[I made an extensive use of scripts based on the srcipts in this page ant they solved a lot of problems for me.
So, many thanks, LucD!
Let ma ask for an additional improvement: while migrating a VM can I specify that I want to change the disk format from thin to thick or from thick to thin?
Regards and thanks again]]></description>
			<content:encoded><![CDATA[<p>I made an extensive use of scripts based on the srcipts in this page ant they solved a lot of problems for me.<br />
So, many thanks, LucD!<br />
Let ma ask for an additional improvement: while migrating a VM can I specify that I want to change the disk format from thin to thick or from thick to thin?<br />
Regards and thanks again</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: LucD		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4627</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sat, 15 Jun 2019 07:38:43 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-4627</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4624&quot;&gt;Bobby&lt;/a&gt;.

Hi Bobby,
These MethodAction tasks just call one method, there is no script involved.
And we would need a script to add more functionality, like writing to a log.
You can schedule a script as well, although on VCSA systems this is a bit more tricky, due to the fact that a VCSA is to be considered a &#039;black box&#039;.

The easiest way, without voiding the support on your VCSA, would be to use an external scheduler.]]></description>
			<content:encoded><![CDATA[<p>Hi Bobby,<br />
These MethodAction tasks just call one method, there is no script involved.<br />
And we would need a script to add more functionality, like writing to a log.<br />
You can schedule a script as well, although on VCSA systems this is a bit more tricky, due to the fact that a VCSA is to be considered a &#8216;black box&#8217;.</p>
<p>The easiest way, without voiding the support on your VCSA, would be to use an external scheduler.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Bobby		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4624</link>

		<dc:creator><![CDATA[Bobby]]></dc:creator>
		<pubDate>Fri, 14 Jun 2019 14:54:38 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-4624</guid>

					<description><![CDATA[Hi Luc,

Thank you for the wonderful and helpful script. I got a question, anyway we can add a status report of this migration to where it will export to a CSV file, perhaps ending on c:\Temp\report.csv/txt?
If like Provisioned size (MB) same from the source?
VMname, disk count mounted? etc.

Thanks,]]></description>
			<content:encoded><![CDATA[<p>Hi Luc,</p>
<p>Thank you for the wonderful and helpful script. I got a question, anyway we can add a status report of this migration to where it will export to a CSV file, perhaps ending on c:\Temp\report.csv/txt?<br />
If like Provisioned size (MB) same from the source?<br />
VMname, disk count mounted? etc.</p>
<p>Thanks,</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jo		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4376</link>

		<dc:creator><![CDATA[Jo]]></dc:creator>
		<pubDate>Wed, 22 Nov 2017 11:02:13 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-4376</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4375&quot;&gt;Jo&lt;/a&gt;.

Nevermind, I did it.
I found this https://www.eehelp.com/question/reconfigure-the-scheduled-task/, which wasn&#039;t working. However, I&#039;ve adapted it and found a way to make it working. Here the extra code:

# Check if the task already exists
	$scheduledTaskManager.RetrieveEntityScheduledTask($vm.MoRef) &#124; %{
		$t = (Get-View ScheduledTaskManager).ScheduledTask &#124; %{ (Get-View $_).Info } &#124; Where-Object {$_.Name -eq $spec.Name}
		$scheduledTask = Get-View -Id $_ &#124; Where-Object {$t.Name -eq $spec.Name}
	}
	
	# If the task already exists then update it, otherwise create it
	if($scheduledTask){
		$scheduledTask.ReconfigureScheduledTask($spec)
	}
	else{
		$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $Spec)
	}]]></description>
			<content:encoded><![CDATA[<p>Nevermind, I did it.<br />
I found this <a href="https://www.eehelp.com/question/reconfigure-the-scheduled-task/" rel="nofollow ugc">https://www.eehelp.com/question/reconfigure-the-scheduled-task/</a>, which wasn&#8217;t working. However, I&#8217;ve adapted it and found a way to make it working. Here the extra code:</p>
<p># Check if the task already exists<br />
	$scheduledTaskManager.RetrieveEntityScheduledTask($vm.MoRef) | %{<br />
		$t = (Get-View ScheduledTaskManager).ScheduledTask | %{ (Get-View $_).Info } | Where-Object {$_.Name -eq $spec.Name}<br />
		$scheduledTask = Get-View -Id $_ | Where-Object {$t.Name -eq $spec.Name}<br />
	}</p>
<p>	# If the task already exists then update it, otherwise create it<br />
	if($scheduledTask){<br />
		$scheduledTask.ReconfigureScheduledTask($spec)<br />
	}<br />
	else{<br />
		$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $Spec)<br />
	}</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jo		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-4375</link>

		<dc:creator><![CDATA[Jo]]></dc:creator>
		<pubDate>Wed, 22 Nov 2017 10:22:53 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-4375</guid>

					<description><![CDATA[Hi Luc,
great article, as always!

As Ryan asked years ago, how can we check if the task already exists and in case remove/edit it?

Thanks]]></description>
			<content:encoded><![CDATA[<p>Hi Luc,<br />
great article, as always!</p>
<p>As Ryan asked years ago, how can we check if the task already exists and in case remove/edit it?</p>
<p>Thanks</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: LucD		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-3900</link>

		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Thu, 19 May 2016 16:45:39 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-3900</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-3897&quot;&gt;Marius&lt;/a&gt;.

In the $arg1.Value you are in fact creating the VirtualMachineRelocateSpec object. In the script I used datastore, pool and host.
Did you already try by just entering the datastore ?]]></description>
			<content:encoded><![CDATA[<p>In the $arg1.Value you are in fact creating the VirtualMachineRelocateSpec object. In the script I used datastore, pool and host.<br />
Did you already try by just entering the datastore ?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marius		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-3897</link>

		<dc:creator><![CDATA[Marius]]></dc:creator>
		<pubDate>Thu, 19 May 2016 09:24:01 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-3897</guid>

					<description><![CDATA[Great code!
I tested it and it works fine for powered off VMs.
My only trouble is: it looks that VMware.Vim.VirtualMachineRelocateSpec always creates a task to change either datastore and host; if I don&#039;t specify anything the current host is kept.
If the VM is powered on the task attempts to change the host instead of changing the datastore.
How can I specify that I want to change only the datastore?
Regards
marius]]></description>
			<content:encoded><![CDATA[<p>Great code!<br />
I tested it and it works fine for powered off VMs.<br />
My only trouble is: it looks that VMware.Vim.VirtualMachineRelocateSpec always creates a task to change either datastore and host; if I don&#8217;t specify anything the current host is kept.<br />
If the VM is powered on the task attempts to change the host instead of changing the datastore.<br />
How can I specify that I want to change only the datastore?<br />
Regards<br />
marius</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marius		</title>
		<link>https://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/#comment-3833</link>

		<dc:creator><![CDATA[Marius]]></dc:creator>
		<pubDate>Wed, 06 Apr 2016 15:48:03 +0000</pubDate>
		<guid isPermaLink="false">http://lucd.info/?p=689#comment-3833</guid>

					<description><![CDATA[Excellent and useful script!
Still valid many years later...
My only concern is that when I review the scheduled task created with the script I see that &quot;Change both host and datastore&quot; is selected, while I want only to change the datastore.
How can I specify that I want to change only the datastore?
Regards]]></description>
			<content:encoded><![CDATA[<p>Excellent and useful script!<br />
Still valid many years later&#8230;<br />
My only concern is that when I review the scheduled task created with the script I see that &#8220;Change both host and datastore&#8221; is selected, while I want only to change the datastore.<br />
How can I specify that I want to change only the datastore?<br />
Regards</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
