<?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>PowerShell Station &#187; Scheduled Tasks</title>
	<atom:link href="http://powershellstation.com/tag/scheduled-tasks/feed/" rel="self" type="application/rss+xml" />
	<link>http://powershellstation.com</link>
	<description>Mike&#039;s PowerShell Musings</description>
	<lastBuildDate>Thu, 05 Apr 2012 03:05:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Getting Scheduled Tasks in PowerShell</title>
		<link>http://powershellstation.com/2009/09/10/getting-scheduled-tasks-in-powershell/</link>
		<comments>http://powershellstation.com/2009/09/10/getting-scheduled-tasks-in-powershell/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 05:12:26 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Scheduled Tasks]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[ETS]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=6</guid>
		<description><![CDATA[There are a few approaches to manipulating scheduled tasks in PowerShell.

    * WMI - Useful if you are only going to manipulate them via script.  The tasks will not be visible in the control panel applet.
    * SCHTASKS.EXE - Works ok, but has a somewhat arcane syntax, and is a text-only tool.
    * Task Scheduler API -Best of both worlds, but only on Vista (not XP).]]></description>
			<content:encoded><![CDATA[<p>There are a few approaches to manipulating scheduled tasks in PowerShell.</p>
<ul>
<li>WMI &#8211; Useful if you are only going to manipulate them via script.  The tasks will not be visible in the control panel applet.</li>
<li>SCHTASKS.EXE &#8211; Works ok, but has a somewhat arcane syntax, and is a text-only tool.</li>
<li>Task Scheduler API -Best of both worlds, but only on Vista (not XP).</li>
</ul>
<p>My current environment is an XP laptop, and hundreds of W2k3 and W2k8 servers.  I really need to be able to hit the tasks from the control panel (don&#8217;t have PowerShell installed everywhere&#8230;but that&#8217;s in process).  Since I have to use SCHTASKS.EXE , I figured I should write a PowerShell interface that makes things a bit easier.</p>
<p>The main thing I wanted was to get objects back.  Once you&#8217;ve started using PowerShell, you realize that &#8220;everything returns objects&#8221; makes for a very powerful scripting experience.  So, when you have to fall back to an external command like SCHTASKS.EXE, you really feel the pain.  The approach I took was to ask SCHTASKS for a verbose output (no reason not to get all of the properties), and also to write the output as CSV.  I then send the output into a file.  A problem that arises when trying to get PowerShell to import the CSV is that the column headers are not very friendly (e.g. &#8220;Repeat: Until: Time&#8221;).  So, before importing we need to &#8220;massage&#8221; a little bit.  I simply removed spaces (leaving Pascal-cased words) and replaced colons with underscores.  Another issue was that different versions of SCHTASKS.EXE would sometimes include an extra blank line in the file.  Finally, I decided that I should add some methods using <strong><span style="font-family: Verdana,sans-serif;">Add-Member</span></strong><span style="font-family: inherit;"> to allow me to run or delete the task without having to remember the syntax. </span></p>
<p>I hope you find this script useful.  If you have suggestions for improvements, or questions about how part of it works, feel free to leave a comment.</p>
<pre class="brush: powershell">function get-tasks($server="", $taskname="",[switch]$help){
  if ($help)
    {
        $msg = @"
Get scheduled tasks from a remote server as objects.  Optionally you may supply a substring to be found in the task names.

Usage: get-tasks [Server] [substring] [-help]
ex:  get-tasks MACHINE1 LOGS    #to get all scheduled tasks from MACHINE1 containing LOGS
"@
        Write-Host $msg
        return
    }
    $filename = [System.IO.Path]::GetTempFileName()
 if ($server){
     schtasks /query /fo csv /s $server /v &amp;gt; $filename
 } else {
    schtasks /query /fo csv /v &amp;gt; $filename
 }
    $lines=Get-Content $filename
 if ($lines -is [string]){
    return $null
 } else {
        if ($lines[0] -ne ''){
   Set-Content -path $filename -Value ([string]$lines[0]).Replace(" ","").Replace(":","_")
   $start=1

  } else {
   Set-Content -path $filename -Value ([string]$lines[1]).Replace(" ","").Replace(":","_")
   $start=2
  }
  if ($lines.Count -ge $start){
   Add-content  -Path $filename -Value $lines[$start..(($lines.count)-1)]
  }
  $tasks=Import-Csv $filename
  Remove-Item $filename
  $retval=@()
  foreach ($task in $tasks){
   if (($taskname -eq '') -or $task.TaskName.contains($taskname)){
    $task.PSObject.TypeNames.Insert(0,"DBA_ScheduledTask")
    Add-Member -InputObject $task -membertype scriptmethod -Name Run -Value { schtasks.exe /RUN /TN $this.TaskName /S $this.HostName}
    Add-Member -InputObject $task -membertype scriptmethod -Name Delete -Value { schtasks.exe /DELETE /TN $this.TaskName /S $this.HostName}
    $retval += $task
   }
  }
  return $retval
 }
}</pre>
<p>Links:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/aa394399%28VS.85%29.aspx">WMI for scheduled tasks (MSDN)</a></li>
<li><a href="http://myitforum.com/cs2/blogs/yli628/archive/2008/07/28/powershell-script-to-retrieve-scheduled-tasks-on-a-remote-machine-task-scheduler-api.aspx">Using the Task Scheduler API (in Vista)</a></li>
<li><a href="http://www.peetersonline.nl/index.php/powershell/managing-scheduled-tasks-remotely-using-powershell/">Another take on scripting SCHTASKS.EXE</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2009/09/10/getting-scheduled-tasks-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  powershellstation.com/tag/scheduled-tasks/feed/ ) in 0.18098 seconds, on May 20th, 2012 at 1:53 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 21st, 2012 at 1:53 am UTC -->
