• subscribe
December 16, 2011 11:39 AM

How-To: Use PowerShell to Report on Scheduled Tasks

An alternative to the Schtasks utility
Windows IT Pro
InstantDoc ID #140978
Downloads
140978.zip

The Windows NT family of OSs has had a built-in program scheduler since its inception. It has grown from the command-line based At scheduler available in Windows NT to the more powerful Task Scheduler service that debuted in Windows 2000. In Windows Server 2008 and Windows Vista, Microsoft overhauled the Task Scheduler service and provided even more functionality.

Windows 2000 didn't provide a script object or command-line interface to the Task Scheduler service, with the exception of the difficult-to-use command-line utility Jt.exe, which was provided in the Microsoft Windows NT Server Resource Kit and not the OS. However, this situation improved starting in Windows XP, which provided the Schtasks utility.

I've seen various requests in newsgroups and online forums asking for a way to create a report of scheduled tasks on one or more computers. I'll describe the OS's built-in way of doing this (namely, Schtasks.exe with the /query parameter), why its output format is difficult to use for reporting purposes, and how I solved this problem using a PowerShell script.

The Problem with Schtasks

As I mentioned previously, XP and later includes the Schtasks utility, which is a command-line interface to the Task Scheduler. The Schtasks command's /query parameter outputs a list of scheduled tasks on a computer. For example, the command

schtasks /query /s server1 /fo CSV 

outputs the scheduled tasks on the computer named server1 in comma-separated value (CSV) format, which is suitable for importing into a spreadsheet or database. The Schtasks /query command works fine on XP and Windows 2003, but if you use it on later versions, you'll run into problems. Vista/Server 2008 and later support task folders, and unfortunately the Schtasks command outputs a separate CSV header row for each task folder, even if a task folder doesn't contain any tasks. Figure 1 shows a sample of CSV data imported from the Schtasks /query /fo CSV command, with the repeated CSV header rows highlighted. It isn't a big problem to delete the extra rows from the output for one computer, but this doesn't scale well when you need to report on scheduled tasks for many computers.

Figure 1: Repeated CSV header rows in the Schtasks command output
Figure 1: Repeated CSV header rows in the Schtasks command output
 

The other Schtasks output formats have problems of their own. The List format (/fo List) provides a newline-separated list, but this is difficult to parse. The Table format (/fo Table) supports a /nh (no headers) option, but there are blank lines in the output and the output is separated by task folders, making it difficult to parse as well. The XML format (/XML) requires writing XML-parsing code to generate a usable report.

Rather than wrestle with parsing the Schtasks command's output, I decided to look for a better way. Fortunately, you can use the Task Scheduler scripting objects (msdn.microsoft.com/en-us/library/aa383607.aspx) to get scheduled task information. I decided to write a PowerShell script, Get-ScheduledTask.ps1, that uses these objects to output scheduled tasks on one or more computers.

Introducing Get-ScheduledTask.ps1 

Get-ScheduledTask.ps1 requires Vista/Server 2008 or later because the TaskService object isn't available on earlier OS versions. You must also run the script from an elevated PowerShell session. (To do this, right-click the PowerShell icon and choose Run as administrator.) The script's syntax is as follows:

Get-ScheduledTask

[[-TaskName] <String[]>]

[[-ComputerName] <String[]>]

[-Subfolders]

[-ConnectionCredential <PSCredential>] 

The -TaskName parameter specifies the name of one or more scheduled tasks. Wildcards (* and ?) are allowed. You can also specify a list of task names separated by commas or a variable containing an array. If you omit this parameter, the default is "*" (i.e., output all tasks). You can omit the -TaskName parameter name if its argument is first on the command line.

The -ComputerName parameter specifies the name of one or more computers. Wildcards aren't allowed with this parameter, but you can specify a text file that contains a list of computer names (one name per line). This parameter also supports pipeline input. If you don't specify a computer name, the current computer is the default. You can omit the -ComputerName parameter name if its argument is second on the command line.

The -Subfolders parameter specifies whether the script supports task subfolders. Without this parameter, the script works only with tasks in the root tasks folder ("\"). In the Task Scheduler GUI, the root tasks folder is the topmost folder in the hierarchy. If a remote computer doesn't support task folders, this parameter is ignored.

The -ConnectionCredential parameter requires a PSCredential object (created with the Get-Credential cmdlet) that contains the credentials you want to use to connect to the TaskService object on the specified computers. Please note that this is a potentially insecure operation. The script must get a plaintext copy of the PSCredential object's password because the TaskService object's Connect method doesn't support encrypted credentials.



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here