Tech Tip: Schtasks
I had a need at work the other day to gather a list of Windows Scheduled Tasks on a variety of servers and aggregate it into a common format. What I found was that Windows already had this capability.
The SCHTASKS command in Windows allows you to access the Windows Scheduled Tasks and AT schedules from the command line and output that information into a common format that can be used in other programs.
Running SCHTASKS /help gives the following syntax help:
SCHTASKS /parameter [arguments] Description: Enables an administrator to create, delete, query, change, run and end scheduled tasks on a local or remote system. Replaces AT.exe. Parameter List: /Create Creates a new scheduled task. /Delete Deletes the scheduled task(s). /Query Displays all scheduled tasks. /Change Changes the properties of scheduled task. /Run Runs the scheduled task immediately. /End Stops the currently running scheduled task. /? Displays this help/usage. Examples: SCHTASKS SCHTASKS /? SCHTASKS /Run /? SCHTASKS /End /? SCHTASKS /Create /? SCHTASKS /Delete /? SCHTASKS /Query /? SCHTASKS /Change /?
If we then look at SCHTASKS /Query /? we get the following syntax help:
SCHTASKS /Query [/S system [/U username [/P password]]] [/FO format] [/NH] [/V] [/?] Description: Enables an administrator to display the scheduled tasks on the local or remote system. Parameter List: /S system Specifies the remote system to connect to. /U username Specifies the user context under which the command should execute. /P password Specifies the password for the given user context. /FO format Specifies the output format to be displayed. Valid values: TABLE, LIST, CSV. /NH Specifies that the column header should not be displayed in the output. Valid only for TABLE and CSV formats. /V Specifies additional output to be displayed. /? Displays this help/usage. Examples: SCHTASKS /Query SCHTASKS /Query /? SCHTASKS /Query /S system /U user /P password SCHTASKS /Query /FO LIST /V /S system /U user /P password SCHTASKS /Query /FO TABLE /NH /V
We see that there are options for retrieving the list of tasks from a specific computer and in a format like CSV which can be imported into a program like Microsoft Excel.
For example, to query MACHINEA and produce a CSV output, the command would be:
SCHTASKS /QUERY /S MACHINEA /FO CSV /V
This would output the following:
"HostName","TaskName","Next Run Time","Status","Last Run Time","Last Result","Creator","Schedule" ,"Task To Run","Start In","Comment","Scheduled Task State","Scheduled Type","Start Time","Start D ate","End Date","Days","Months","Run As User","Delete Task If Not Rescheduled","Stop Task If Runs X Hours and X Mins","Repeat: Every","Repeat: Until: Time","Repeat: Until: Duration","Repeat: Sto p If Still Running","Idle Time","Power Management" "MACHINEA","GoogleUpdateTaskMachineCore","At logon time","","22:46:10, 2/23/2010","0","tom","Run at user logon","C:\Program Files\Google\Update\GoogleUpdate.exe /c","N/A","Keeps your Google software up to date. If this task is disabled or stopped, your Google software will not be kept up to date, meaning security vulnerabilities that may arise cannot be fixed and features ma y not work. This task uninstalls itself when the","Enabled","At logon time","At logon time","1/1/ 1999","N/A","N/A","N/A","NT AUTHORITY\SYSTEM","Disabled","Disabled","Disabled","Disabled","Disabl ed","Disabled","Disabled","Disabled" "MACHINEA","GoogleUpdateTaskMachineCore","12:50:00, 2/24/2010","","22:46:10, 2/23/2010","0 ","tom","At 12:50 PM every day, starting 1/15/2010","C:\Program Files\Google\Update\GoogleUpd ate.exe /c","N/A","Keeps your Google software up to date. If this task is disabled or stopped, yo ur Google software will not be kept up to date, meaning security vulnerabilities that may arise c annot be fixed and features may not work. This task uninstalls itself when the","Enabled","Daily ","12:50:00","1/15/2010","N/A","Everyday","N/A","NT AUTHORITY\SYSTEM","Disabled","Disabled","Disa bled","Disabled","Disabled","Disabled","Disabled","Disabled" "MACHINEA","GoogleUpdateTaskMachineUA","23:50:00, 2/23/2010","","22:50:00, 2/23/2010","0", "tom","Every 1 hour(s) from 12:50 PM for 24 hour(s) every day, starting 1/15/2010","C:\Progra m Files\Google\Update\GoogleUpdate.exe /ua /installsource scheduler","N/A","Keeps your Google sof tware up to date. If this task is disabled or stopped, your Google software will not be kept up t o date, meaning security vulnerabilities that may arise cannot be fixed and features may not work . This task uninstalls itself when the","Enabled","Hourly ","12:50:00","1/15/2010","N/A","Everyda y","N/A","NT AUTHORITY\SYSTEM","Disabled","Disabled","1 Hour(s)","None","24 Hour(s): 0 Minute(s)" ,"Disabled","Disabled","Disabled"
To send this into a file instead of the screen, you can use the redirection operator “>” with a filename “> filename.csv” to send the output to filename.csv which can then be opened in another program.
This helpful for a single server, but I did not want to have to type this for each server I wanted to query, so I wrote the following Powershell script to take a list of servers from a text file, query their tasks and then output it to a single file that I could reference later.
# Powershell Script to Query Remote Servers and Display Scheduled Tasks
# 2/18/2010 - Tom Determan
# Count and Display Number of Servers in Input FIle
$NUMSERVERS = (get-content d:\data\servers.txt | Measure-Object -property length -Line)
write-host "Number of Servers to Check: " $NUMSERVERS.Lines
set-variable -name COUNT -value 0
# Query Each Machine and Output CSV of Scheduled Tasks
get-content d:\data\servers.txt | Foreach-Object {
$COUNT=$COUNT+1
set-variable -name SERVERNAME -value $_
Write-Host "Checking Server: " $SERVERNAME " " $COUNT " of " $NUMSERVERS.Lines
# For first line print column headings, other lines omit /NH option in schtasks
if ($COUNT -eq 1) {schtasks /query /FO CSV /V /S $SERVERNAME | Out-File d:\data\output1.csv}
else {schtasks /query /NH /FO CSV /V /S $SERVERNAME | Out-File -append d:\data\output1.csv}
}
# schtasks outputs blank line at beginning, go through and remove blank lines from final file
get-content d:\data\output1.csv | where {$_ -ne ""} > d:\data\output.csv
erase d:\data\output1.csv
If you find this useful feel free to use the script. If you find a better way to accomplish what I set out to do, shoot me an email and let me know. I was able to get this to work on Windows 2000 through Windows 2008 Servers.
Permalink Comments off