<?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>PeetersOnline &#187; scheduled tasks</title>
	<atom:link href="http://www.peetersonline.nl/index.php/tag/scheduled-tasks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.peetersonline.nl</link>
	<description>Using Powershell for (virtually) everything!</description>
	<lastBuildDate>Fri, 19 Aug 2011 09:22:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Managing Scheduled Tasks Remotely Using Powershell</title>
		<link>http://www.peetersonline.nl/index.php/powershell/managing-scheduled-tasks-remotely-using-powershell/</link>
		<comments>http://www.peetersonline.nl/index.php/powershell/managing-scheduled-tasks-remotely-using-powershell/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 12:40:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[scheduled tasks]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://www.peetersonline.nl/?p=395</guid>
		<description><![CDATA[The following Powershell functions allow you to manage querying, creating and removing scheduled tasks on one or more computers remotely. The functions use schtasks.exe, which is included in Windows. Unlike the Win32_ScheduledJob WMI class, the schtasks.exe commandline tool will show manually created tasks, as well as script-created ones. The examples show some, but not all <a href='http://www.peetersonline.nl/index.php/powershell/managing-scheduled-tasks-remotely-using-powershell/'>[...]</a>
Related posts:<ol>
<li><a href='http://www.peetersonline.nl/index.php/vmware/remove-vmware-memory-limits-with-powershell/' rel='bookmark' title='Remove vmware memory limits with Powershell'>Remove vmware memory limits with Powershell</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The following <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target='_blank'>Powershell</a> functions allow you to manage querying, creating and removing scheduled tasks on one or more computers remotely.<br />
The functions use schtasks.exe, which is included in Windows. Unlike the Win32_ScheduledJob WMI class, the schtasks.exe commandline tool will show manually created tasks, as well as script-created ones. The examples show some, but not all parameters in action. I think the parameter names are descriptive enough to figure it out, really. If not, take a look at schtasks.exe /?. One tip: try piping a list of computer names to foreach-object and into this function.</p>
<p><span id="more-395"></span><br />
Enjoy.<br />
Hugo</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">Function</span> Get<span style="color: pink;">-</span>ScheduledTask
	<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ComputerName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Computer: $ComputerName&quot;</span>
	<span style="color: #800080;">$Command</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;schtasks.exe /query /s $ComputerName&quot;</span>
	<span style="color: #008080; font-weight: bold;">Invoke-Expression</span> <span style="color: #800080;">$Command</span>
	<span style="color: #008080; font-weight: bold;">Clear-Variable</span> Command <span style="color: #008080; font-style: italic;">-ErrorAction</span> SilentlyContinue
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># EXAMPLE: Get-ScheduledTask -ComputerName Server01</span>
&nbsp;
<span style="color: #0000FF;">Function</span> Remove<span style="color: pink;">-</span>ScheduledTask
	<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ComputerName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$TaskName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;blank&quot;</span>
	<span style="color: #000000;">&#41;</span>
	<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>ScheduledTask <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$ComputerName</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$TaskName</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #800000;">&quot;Are you sure you want to remove task $TaskName from $ComputerName(y/n)&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;y&quot;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Command</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;schtasks.exe /delete /s $ComputerName /tn $TaskName /F&quot;</span>
			<span style="color: #008080; font-weight: bold;">Invoke-Expression</span> <span style="color: #800080;">$Command</span>
			<span style="color: #008080; font-weight: bold;">Clear-Variable</span> Command <span style="color: #008080; font-style: italic;">-ErrorAction</span> SilentlyContinue
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">Else</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Warning</span> <span style="color: #800000;">&quot;Task $TaskName not found on $ComputerName&quot;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># EXAMPLE: Remove-ScheduledTask -ComputerName Server01 -TaskName MyTask</span>
&nbsp;
<span style="color: #0000FF;">Function</span> Create<span style="color: pink;">-</span>ScheduledTask
	<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ComputerName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$RunAsUser</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;System&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$TaskName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;MyTask&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$TaskRun</span> <span style="color: pink;">=</span> <span style="color: #800000;">'&quot;C:\Program Files\Scripts\Script.vbs&quot;'</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Schedule</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Monthly&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Modifier</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;second&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Days</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SUN&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Months</span> <span style="color: pink;">=</span> <span style="color: #800000;">'&quot;MAR,JUN,SEP,DEC&quot;'</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$StartTime</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;13:00&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$EndTime</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;17:00&quot;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Interval</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;60&quot;</span>	
	<span style="color: #000000;">&#41;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Computer: $ComputerName&quot;</span>
	<span style="color: #800080;">$Command</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;schtasks.exe /create /s $ComputerName /ru $RunAsUser /tn $TaskName /tr $TaskRun /sc $Schedule /mo $Modifier /d $Days /m $Months /st $StartTime /et $EndTime /ri $Interval /F&quot;</span>
	<span style="color: #008080; font-weight: bold;">Invoke-Expression</span> <span style="color: #800080;">$Command</span>
	<span style="color: #008080; font-weight: bold;">Clear-Variable</span> Command <span style="color: #008080; font-style: italic;">-ErrorAction</span> SilentlyContinue
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># EXAMPLE: Create-ScheduledTask -ComputerName MyServer -TaskName MyTask02 -TaskRun &quot;D:\scripts\script2.vbs&quot;</span></pre></div></div>

<p>Related posts:<ol>
<li><a href='http://www.peetersonline.nl/index.php/vmware/remove-vmware-memory-limits-with-powershell/' rel='bookmark' title='Remove vmware memory limits with Powershell'>Remove vmware memory limits with Powershell</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.peetersonline.nl/index.php/powershell/managing-scheduled-tasks-remotely-using-powershell/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

