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

I want a list of scheduled tasks for the member servers in my Active Directory (AD) domain. The list should include the user accounts the servers use to run the tasks. I also want to be able to import the list into Microsoft Excel. How can I do this?

I'll answer this question in two parts. First, I explain how to get an appropriate list of computers by searching AD. Second, I show you how to get the scheduled tasks from each computer in the list. Microsoft provides an excellent overview of how to search AD in the "Microsoft Windows 2000 Scripting Guide" (http://www.microsoft.com/technet/scriptcenter/guide). Briefly, using a script to search AD requires the following steps:

  1. Create a Connection object (ADODB.Connection) and use its Open method to establish a connection to AD.
  2. Create a Command object (ADODB.Command) and set its ActiveConnection property to the open Connection object.
  3. Set the Command object's CommandText property to an appropriate LDAP query string.
  4. Use the Command object's Execute method to return a RecordSet object.
  5. Close the Connection object.

The ListComputers.vbs script in Listing 1 uses VBScript to list all Computer objects in the current domain. The code at callout A in Listing 1 uses the rootDSE object to retrieve the current domain's name, and the LDAP query at callout B selects the common name (CN) property (i.e., just the computer name) for all Computer objects. The code at callout C iterates the RecordSet and echoes the Value property of the CN field for each record. The CN field (or column) exists because that's the property that the LDAP query at callout B specifies.

Getting a list of domain controllers (DCs) requires a different approach. Listing 2's script, ListDCs.vbs, shows how to use VBScript to get the list. The code at callout A in Listing 2 uses the rootDSE object to return the current domain's configuration naming context, and the LDAP query at callout B selects the AdsPath property for all nTDSDSA objects. In the AD hierarchy, the nTDSDSA object's parent is a DC. The code at callout C iterate the RecordSet, retrieves the Parent object of each nTDSDSA object, and echoes the Parent object's CN property (i.e., the DC's name).

Because AD doesn't have attributes that store a computer's role (e.g., workstation, member server, DC), you can't include such attributes in a query. To work around this limitation, you can include the operatingSystem attribute in the LDAP query. If the operatingSystem attribute contains the word "server," then the computer is a server. However, note that DCs will be included in such a search.

A script can generate a member server list that doesn't include DCs by first retrieving a list of DCs. After it gets the list of DCs, the script can retrieve a list of Computer objects. If a Computer object's operatingSystem attribute contains the word "server" and the Computer object's name isn't in the list of DCs, the computer is a member server. I've written a complete script, EnumComputers.js, that performs this task for you. It's too long to print here, but you can download it from the Windows Scripting Solutions Web site at http://www.windowsitpro.com/windowsscripting, Instant-Doc ID 50325.

The EnumComputers.js script has the following command-line syntax:

[cscript] EnumComputers.js /wks 
| /dc | /srv [/dc]
EnumComputers.js is intended to run in a command window, so it requires the CScript host. The cscript keyword at the beginning of the command line is required only if CScript isn't your default host. To set CScript as your default host, type into a command window the following command:
 cscript //h:cscript //nologo //s 

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.