Mr. Roboto

Bionic Scheduled Task Reporting

Mr. Roboto revamps a previous script to help a user retrieve and save scheduled task information from multiple computers -- all using PowerShell.

Last year, I wrote a column that offered a Visual Basic Scripting Edition-based HTML application (HTA) to report scheduled tasks on multiple remote computers. The tool was essentially a wrapper for the Schtasks.exe command-line tool that ships with Windows XP.

A post in the Windows PowerShell forum at ScriptingAnswers.com recently brought up this topic. The poster was trying to get scheduled task information from a list of computers, which he then wanted to save as a comma-separated value (.CSV) file. Oh, and he wanted to use PowerShell. We worked out a solution, and I took it a step further for you.

Here's What You Do
First, go to jdhitsolutions.com/scripts and download the .ZIP file with the scheduled task scripts. There are two: Get-SimpleScheduledTask.ps1 and Get-ScheduledTask.ps1. Both scripts are written for PowerShell version 1, but there's no reason why they shouldn't run under the current PowerShell version 2 community technology preview. There are no native cmdlets for managing scheduled tasks, so we're still reliant on Schtasks.exe. Both scripts take the output from Schtasks.exe and create objects that you can use in a PowerShell-pipelined expression. Both scripts also take a computer name as a parameter and default to the local host.

These scripts require that they run on Windows XP or Windows Server 2003, and that they query one of these two platforms or Windows 2000. Microsoft changed the Task Scheduler architecture with Windows Vista and Windows Server 2008. They are incompatible with the older version of Schtasks.exe and, by extension, with these scripts.

Script No. 1
Get-SimpleScheduledTask.ps1, the first script, is a wrapper for a simple scheduled task query:

C:\ schtasks /query /s server12

The script takes it a step further by providing warning messages for connection errors. This Schtasks.exe syntax only returns Taskname, Next Run Time and Status. The PowerShell script inserts the computer name into the output so you can use a PowerShell expression like this one:

PS C:\ get-content 
c:\files\servers.txt | foreach 
{c:\scripts\Get-SimpleScheduled 
Task.ps1 $_} | Sort Hostname | 
ConvertTo-html -title "Scheduled 
Tasks" | out-file 
\\file01\reports\schtaskrprt.html

Because the script is sending an object to the pipeline, you can use any appropriate PowerShell cmdlet. In this example, I'm creating a sorted report with simple task information that is converted to an HTML report.

Script No. 2
For more detailed task information, you'll want to use the second script, Get-ScheduledTask.ps1. This script takes the output of a command like this and turns it into PowerShell objects:

C:\ schtasks /query /s server12 /v

Running a command like this, you'll see about 20 task details. This command creates a custom object for each scheduled task using the details as property values.

PS C:\Scripts\> .\Get-Scheduled 
Task.ps1

I did make one minor change that you should be aware of. Even though you can have property names with spaces, I went through and removed them; thus, a scheduled task detail like Next Run Time becomes NextRunTime. For example:

PS C:\Scripts\> .\Get-Scheduled 
Task | select Task*,Last*,NextRunTime

The Benefits
If you only wanted select information, the command-line version of Schtasks.exe would be tedious to parse-but the PowerShell approach offers flexibility:

PS C:\ get-content c:\files\servers.txt | foreach { c:\scripts\Get-SimpleScheduled Task.ps1 $_} | sort Hostname | for mat-list HostName,TaskName,NextRun Time,Status,RunAsUser,Creator | out-file \\file01\reports\schtaskrpt.txt

I hope that as you work with these scripts, you'll appreciate how easy it is to use PowerShell, and that you won't have to sacrifice existing tools.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

Featured

comments powered by Disqus

Subscribe on YouTube