Searching for Published Folders in Active Directory

Microsoft® Windows® 2000 Scripting Guide

Retrieving a list of all the folders that are published in Active Directory is obviously useful. At the same time, in a large organization a list such as this might contain hundreds or even thousands of shares. Having to read through a list that large to find a particular folder or two would be very inefficient.

Fortunately, you can also search Active Directory for only those published folders that meet specific criteria. For example, you might search for a folder named FinanceShare, for all the folders managed by Ken Myer, or for all the folders that have "finance" as one of their keywords. By combining criteria, you can create targeted searches that help you quickly and easily locate only the published folders you are most interested in.

Scripting Steps

Listing 11.40 contains a script that searches for a published folder in Active Directory. To carry out this task, the script must perform the following steps:

  1. Include the On Error Resume Next statement. Without this statement, the script fails if it is unable to find any published folders in Active Directory.

  2. Create a constant named ADS_SCOPE_SUBTREE and set the value to 2. This is used to specify a search that begins in the Active Directory root and proceeds to search all the child containers as well.

  3. Create an instance of the Active Directory connection object (ADODB.Connection).

  4. Create an instance of the Active Directory command object (ADODB.Command).

    The command object allows you to issue queries and other database commands using the Active Directory connection.

  5. Set the provider property of the connection object to the Active Directory provider (ADsDSOObject). This is the OLE database provider for ADSI.

  6. Set the active connection to the Active Directory connection.

  7. Set the command text for the Active Directory command object to the SQL query that retrieves all the published folders from fabrikam.com.

    To limit data retrieval to a specific set of shared folders, a Where clause is included that restricts the returned collection to those shared folders that include the keyword "finance."

  8. Specify values for time-out, search scope, and caching.

  9. Execute the SQL query.

  10. When the set of published shared folders is returned, use the MoveFirst method to move to the first share in the recordset.

  11. For each share in the recordset, echo the share name, the UNC name, and the name of the user responsible for managing the share.

Listing 11.40 Searching for a Shared Folder in Active Directory

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT Name, unCName, ManagedBy FROM " _
 & "'LDAP://DC=fabrikam,DC=com'" _
 & " WHERE objectClass='volume' AND Keywords = 'finance*'"
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
 Wscript.Echo "Share Name: " & objRecordSet.Fields("Name").Value
 Wscript.Echo "UNC Name: " & objRecordSet.Fields("uNCName").Value
 Wscript.Echo "Managed By: " & objRecordSet.Fields("ManagedBy").Value
 objRecordSet.MoveNext
Loop