Thursday, December 01, 2011

Windows Scheduled Tasks Inventory

Here is another script which goes through each my Windows Servers and list out scheduled tasks and the credentials use to run them.  I needed to know what are the scheduled tasks running on each server and most importantly what accounts are used to run them.


Initially, I was looking at using the Win32_ScheduledJob WMI class.  However, it does not work well for me because it only represents a job/task created with the AT command.  Job/Task created with the Scheduled Task Wizard from the Control Panel cannot be found in this class.  Check for more information.
So with some searching, I found this script which display task name and status.  I modified the script to get what I needed .
On Error Resume Next
Dim rootFolder
Dim taskCollection
Dim numberOfTasks
Dim taskDefinition
Dim principal
Dim registeredTask

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objServerFile = objFSO.OpenTextFile("servers.txt", ForReading)
strFileName = "ScheduleTasks.txt"
Set objFileResult = objFSO.OpenTextFile(strFileName, 8, True, 0)
objFileResult.WriteLine "Server Name" + vbTab + "Service Account" + vbTab + "Task Name"

Do Until objServerFile.AtEndOfStream
    strServerName = objServerFile.Readline
    ' Create the TaskService object.
    Set service = CreateObject("Schedule.Service")
    call service.Connect(strServerName)
    Wscript.echo "Connecting to " & strServerName
    'Catch the error connecting to the server
    If Err.Number <> 0 Then
        Wscript.echo strServerName + vbTab + "Error Connecting" + vbTab + Err.Description
        objFileResult.WriteLine strServerName + vbTab + "Error Connecting" + vbTab + Err.Description
        ' Get the task folder that contains the tasks.
        Set rootFolder = service.GetFolder("\")
        Set taskCollection = rootFolder.GetTasks(0)
        numberOfTasks = taskCollection.Count
        If numberOfTasks = 0 Then
            Wscript.Echo "No tasks are registered."
                objFileResult.WriteLine strServerName + vbTab + "None" + vbTab + "No Task"
            WScript.Echo "Number of tasks registered: " & numberOfTasks
                'Loop trhough the task collection to get the task name and account used to run the task
            For Each registeredTask In taskCollection           
                Set taskDefinition = registeredTask.Definition
                 Set principal = taskDefinition.Principal
            WScript.Echo "Task Name: " & registeredTask.Name
            Wscript.Echo "User Name: " & principal.UserId
                objFileResult.WriteLine strServerName + vbTab + principal.UserId + vbTab + registeredTask.Name          
        End If   
    End If
'Clean up
The script will read from a file servers.txt to get the list of servers you want to go through.
Example of servers.txt:
Open a command prompt with an account that has administrative rights over the servers that you want to go through.  Run the script using cscript.exe.
C:\scripts>cscript ScheduleTasksInventory.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Connecting to deServer1
Number of tasks registered: 3
Task Name: At1
User Name:
Task Name: WebTest
User Name: deInfoTech\WebAdmin
Task Name: Note
User Name: SYSTEM
Connecting to deServer2
Number of tasks registered: 1
Task Name: movetoBackup
User Name: SYSTEM
The results will be written to ScheduleTasks.txt which is tab delimited.  Below is a sample of the results opened in Excel.
Server Name Service Account Task Name
deServer1   At1
deServer1 deInfoTech\WebAdmin WebTest
deServer1 SYSTEM Note
deServer2 SYSTEM movetoBackup
Using the Schedule.Service object, it does list out the task created using AT Command but the account used to run the task is missing.  Nevertheless, this does not seem to be a problem because it looks like tasks created using AT Command are running using the NT Authority\System account.  Again, this has saved me the hassle and time needed to go through each server manually.

No comments: