Subscribe to Windows IT Pro
June 12, 2006 12:00 AM

Using VBScript to List Member Servers' Scheduled Tasks

Windows IT Pro
InstantDoc ID #50325
Rating: (0)
Downloads
50325.zip

EnumComputers.js returns a list of computer names, one per line. The /wks option lists computers that don't have the word "server" in the operatingSystem attribute. The /dc option lists only DCs. And the /srv option uses the technique I described previously to list only member servers. If you use both the /srv and /dc options, EnumComputers.js will list all servers, including member servers and DCs.

Now that we have a script that can retrieve a list of member servers, we can move on to the second part of the question: getting a list of scheduled tasks for the member servers. Complicating the answer is the fact that Windows provides two different APIs for managing scheduled tasks: the At and Task Scheduler APIs. The At command and the WMI Win32_ScheduledJob interface can modify At API tasks, but Microsoft doesn't provide a script object for managing Task Scheduler API tasks.

However, all is not lost because the Schtasks command-line tool provides a method for enumerating Task Scheduler API tasks from the command line. If you include the /v (verbose output) option, the Schtasks /query command returns the user account that's configured to run each task.

Listing 3's Cmd.exe shell script (batch file), EnumTasks.cmd, lists scheduled tasks for each computer in a text file. The script first makes sure that you've provided a command-line argument. Then, the script sets the LISTFILE variable equal to the first argument on the command line, stripping any leading and trailing double quotes in the process. If the file containing the list of computers (which is the text file pointed at by the LISTFILE variable) doesn't exist, the script exits with a non-zero exit code, as the code at callout A in Listing 3 shows.

Because EnumTasks.cmd will simply execute a Schtasks /query command for each line in the text file, the script provides a comma-separated value (CSV) header line to begin its output. Then, EnumTasks.cmd uses the For /f command to parse each line of the text file and calls the ENUM procedure for each line of the file, as the code at callout B shows.

The code at callout C in Listing 3 contains the ENUM procedure. This section of code first creates a temporary filename by calling the TEMPNAME procedure, then calls the ALIVE procedure to determine whether the computer responds to a ping request. If the computer doesn't respond, the script returns the computer name and the message NO REPLY as CSV text and jumps to the end of the ENUM procedure. Next, the ENUM procedure executes the Schtasks command and redirects its output to the temporary file. If the Schtasks command returns a nonzero exit code, the procedure returns the computer name and the message ERROR as CSV text, then jumps to the end of the procedure. Next, the procedure uses the Findstr command to determine whether the temporary file contains at least one double-quote character ("). If the double-quote character doesn't exist in the temporary file, the ENUM procedure returns the computer name and the message NO TASKS as CSV text, then jumps to the end of the procedure.

At this point, the ENUM procedure assumes that the temporary file contains valid CSV text, including a list of the scheduled tasks for the computer. The procedure uses the For /f command to skip the first two lines of the temporary file (the first line is blank, and the second line contains column headings) and displays the remainder of the lines in the file. The ENUM procedure then deletes the temporary file and returns control to the main body of the script.

You can use EnumComputers.js with EnumTasks.cmd to accomplish your goals. First, to use EnumComputers.js to get a list of servers in a text file, run the following command:

EnumComputers.js /srv > 
  Servers.txt  

Next, to use EnumTasks.cmd to read the list of servers and redirect the previous command's output to a CSV file, run the following command:

EnumTasks Servers.txt >  
  ServerTasks.csv 

You can then use a database or spreadsheet tool such as Excel to import the CSV file for processing.

Related Content:

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

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.