What Is Global Catalog?
As you become more proficient in writing your scripts, and as you begin to work with the enterprise on a global scale, you will begin to wonder why some queries seem to take forever and others run rather quickly. After configuring some of the parameters you looked at earlier, you might begin to wonder whether you're querying a Global Catalog server. A Global Catalog server is a server that contains all the objects and their associated attributes from your local domain. If all you have is a single domain, it doesn't matter whether you're connecting to a domain controller or a Global Catalog server, because the information would be the same. If, however, you are in a multiple domain forest, you might very well be interested in which Global Catalog server you are hitting. Depending on your network topology, you could be executing a query that is going across a slow wide area network (WAN) link. You can control replication of attributes by selecting the Global Catalog check box. You can find this option by opening the Active Directory Schema MMC, highlighting the Attributes container, and then double-clicking the attribute you want to modify. You will then be presented with the form shown in Figure 13-1.

In addition to controlling the replication of attributes, the administrator might also investigate attribute indexing. (See Figure 13-2.) Active Directory already has indexes built on certain objects.

Warning  | The Active Directory Schema MMC tool is not available by default. You must first register the schmmgmt.dll tool using Regsvr32. Changes made to the Active Directory Schema can only be made by members of the Schema Admins group in Active Directory. If you are a member of that group, then the Active Directory Schema MMC tool is much more dangerous than Regedit, because it will permit changes to the schema. Modifying the Active Directory Schema should only be undertaken when one is fully cognizant of the implications from both a performance and a security standpoint. |
However, if an attribute is heavily searched on, you might consider an additional index. You should do this, however, with caution, because an improperly placed index is worse than no index at all. The reason for this is the time spent building and maintaining an index. Both of these operations use processor time and disk input/output (I/O).
Suppose you create a custom attribute called badgeNumber in Active Directory. This attribute would be a small number with a high degree of cardinality. Cardinality is a database term that refers to the level of uniqueness of the data. High cardinality implies greater uniqueness. For example, in most cases, the givenName field in Active Directory will have a low level of cardinality because several users are likely to have the popular first names of Bob, Alice, Sally, Teresa, and Ed. On the other hand, only one user in Active Directory is associated with a particular employee number and therefore the employeeNumber field has a high level of cardinality. EmployeeNumber, then, would be a good candidate for indexing.
However, just because a field is a good candidate for indexing doesn't mean it should be indexed. It simply means it could be indexed. Before you decide to select the check box for the badgeNumber attribute, for example, decide how often you'll search on users by badge number. To help you figure this out, you could audit LDAP queries that are performed against Active Directory.
Querying a Global Catalog server
1. | Launch Notepad or your favorite script editor and open \My Documents\Microsoft Press\VBScriptSBS\Utilities\BasicQueryTemplate.vbs. Save it as YourNameQueryGC.vbs.
| 2. | At the bottom of the template, find the section of the funfix function that builds the connection string into Active Directory. It will look like the following:
If strOU <> vbempty Then
funfix = "<LDAP://" & strOU & "," & funfix & ">"
else
funfix = "<LDAP://" & funfix & ">"
End if
| 3. | Change LDAP to GC in both places in the function. The modified code is seen below:
If strOU <> vbempty Then
funfix = "<GC://" & strOU & "," & funfix & ">"
else
funfix = "<GC://" & funfix & ">"
End if
| 4. | Save and run the script. If the script does not run properly, compare you script with \My Documents\Microsoft Press\VBScriptSBS\ch13\QueryGC.vbs. Remember, you will want to run this script under Cscript and make sure it points to the appropriate domain on your network.
|
Querying a specific server
1. | Open the ADOSearchTemplate.vbs in the Utilities folder and save the file as YourNameSpecificSearchServer.vbs.
| 2. | Identify the LDAP connection line, seen below:
strQuery = "<LDAP://ou=mred,dc=nwtraders,dc=msft>;" _
| 3. | After LDAP:// add the name of your server. Make sure it is before the ou= statement, and that it is followed by a trailing /. This is seen below:
strQuery = "<LDAP://London/ou=mred,dc=nwtraders,dc=msft>;" _
| 4. | Compare your completed strQuery code with the one listed below:
strQuery = "<LDAP://London/ou=mred,dc=nwtraders,dc=msft>;" _
& "(objectCategory=user);" _
& "name;" _
& "subtree"
| 5. | Save and run your script. If it does not produce the expected results, compare your script with the \My Documents\Microsoft Press\VBScriptSBS\ch13\SpecificSearchServer.vbs script.
|
 |
Q. | Why would a local Global Catalog server not be used in responding to a query?
| A. | One reason could be that the Global Catalog server does not contain the attribute for which you were searching. If it does not contain the attribute, it must refer the query to another server. | Q. | What are the main questions a network administrator must answer prior to indexing an attribute in Active Directory?
| A. | A network administrator should look at the size of the data field, the level of cardinality, and the amount of use the attribute will generate as a search criterion. |
|
|
Using ADO to query a Microsoft Office Excel spreadsheet
1. | Launch Notepad or your favorite script editor and open the \My Documents\Microsoft Press\VBScriptSBS\Utilities\ADOSearchTemplate.vbs script. Save it as YourNameQueryExcelUsingADO.vbs.
| 2. | In the Header section of the script, declare four new variables. These variables will hold the provider name, the data source statement, the extended attributes for Excel, and the name of the file. They are listed below:
Dim strProvider
Dim strDataSource
Dim strExtend
Dim strFileName
| 3. | Specify the Jet provider to use to open Excel. It is the Microsoft.Jet.OLEDB.4.0, and you will assign it to the strProvider variable, as seen below:
strProvider = "Provider=Microsoft.Jet.OLEDB.4.0"
| 4. | Tell the provider to use the extended properties for Microsoft Office Excel 8.0 (this is true even if you are using Microsoft Office Excel 2003) and assign the extended properties string to the strExtend variable.
strExtend = "Extended Properties=Excel 8.0"
| 5. | Assign the spreadsheet name (in this case, wmiProvidersXP.xls in the Chapter 13 folder) to the strFileName variable. Use the funfix function to pad the file name with double quotation marks, as seen below:
strFileName = funfix("WMIprovidersXP.xls")
| 6. | Assign the file name string contained in the strFileName variable to the strDataSource variable, as seen below:
strDataSource = "Data Source =" & strFileName
| 7. | Modify the current line to assign the query to the strQuery variable. Use a Select statement to do this. The sheet name must be surrounded with square brackets, and the name of the sheet is appended with a dollar sign, as seen below:
strQuery = "Select * from [CIMV2$]"
| 8. | In the Worker section of the script, modify the objConnection.Open statement so that it is using the provider specified in the strProvider variable, the data source from strDataSource, and the extended attributes contained in strExtended. Each of these will be concatenated with the other, and separated by semicolons, as seen below:
objConnection.Open strProvider & ";" & strDataSource &";" & strExtend
| 9. | Modify the Output section of the script by editing the field name to reflect the column of interest (class name) from the spreadsheet. This is seen below:
WScript.Echo objRecordSet.Fields("Class Name")
| 10. | Create the funfix function at the bottom of the script. This function will simply accept an input parameter called strIN and append single quotation marks to the front and back of the value. This is seen below:
Function funfix (strIN)
funfix = "'" & strIN & "'"
End Function
| 11. | Save and run the script. If it does not produce a list of WMI classes, compare the script to \My Documents\Microsoft Press\VBScriptSBS\ch13\QueryExcelUsingADO.vbs.
|
Using ADO to query a Microsoft Office Access database
1. | Open \My Documents\Microsoft Press\VBScriptSBS\ch13\QueryExcelUsingADO.vbs in Notepad or your favorite script editor. Save the file as YourNameQueryAccessUsing ADO.vbs.
| 2. | Delete the line that declares the strExtend variable that was used to hold the extended properties required for querying Excel. The line to delete is seen below.
| 3. | Delete the line that assigns the extended properties for Excel to the strExtend variable. This line is seen below.
strExtend = "Extended Properties=Excel 8.0"
| 4. | Type in the name of the Access database you want to query. In this case, you will use the EmployeesTest.mdb file included in the Chapter 13 folder. Assign the file name to the strFileName variable, but feed it into the funfix function that is already present on the line. This function will append and prepend the single quotation marks required by the Datasource property.
strFileName = funfix("EmployeesTest.mdb")
| 5. | Modify the query contained in the strQuery variable so that you select everything from the Employees table. The code to do this is seen below:
strQuery = "Select * from Employees"
| 6. | Modify the objConnection.Open line, as seen below:
objConnection.Open strProvider & ";" & strDataSource
| 7. | Modify the WScript.Echo command in the Output section of the script so it will print out both the first name and the city of each employee in the database. Combine the two fields on a single line using a single echo command. This line will look like the following:
WScript.Echo objRecordSet.Fields("firstName"), objRecordSet.Fields("city")
| 8. | Save and run the script. You should see an output similar to that listed below. If it does not appear, then compare your script with the \My Documents\Microsoft Press\VBScriptSBS\ch13\QueryAccessUsingADO.vbs script.
sam memphis
sally atlanta
sarah hollywood
jose charlotte
| 9. | Modify the output command to remove the .Fields property. You do this by simply deleting the .Fields portion of the output. Naming this property in a record set is optional because it is the default property. This modification is seen below:
WScript.Echo objRecordSet("firstName"), objRecordSet("city")
| 10. | Save and run the script. The output should be the same as the output from the previous time you ran the script. If it is not, then compare it with the \My Documents\Microsoft Press\VBScriptSBS\ch13\QueryAccessUsingADO.vbs script.
|
Using ADO to query a text file
1. | Open the \My Documents\Microsoft Press\VBScriptSBS\ch13\QueryExcelUsing ADO.vbs script in Notepad or your favorite script editor and save it as YourNameQueryTextFileUsingADO.vbs.
| 2. | Declare a new variable called strPath that will contain the path to the text file to query.
| 3. | Modify the value of strExtended to provide the extended properties needed to query a text file. These attributes tell ADO that datasource is a text file, and we are specifying a header to the file that is delimited. This line of code is seen below:
strExtend = "Extended Properties=""text;HDR=YES;FMT=Delimited"""
| 4. | After the strExtend line, add a line to hold the value of strPath. This is the folder that contains the text file to query. In this case, it is c:\fso, as seen below:
| 5. | Delete the funfix function from the bottom of the script. Also delete the reference to funfix from the strFileName variable in the Reference section of the script. The new file to query is called MyText.csv. This is the new value of strFileName, as seen below:
strFileName = "myText.csv"
| 6. | The strDataSource variable will hold the path to the file, not the file name itself. Edit the assignment to strDataSource so it contains the strPath variable, as seen below:
strDataSource = "Data Source =" & strPath
| 7. | To query a text file using ADO, we only need to use Select * from the name of the text file. Because the text file is contained in the strFileName variable, the query references strFileName instead of the square bracketed name of the Excel spreadsheet that was present in the original script. Edit the strQuery line to reflect this convention.
strQuery = "Select * from " & strFileName
| 8. | Modify the Output section of the script. We are interested in printing out the name, address, and phone number of each employee. One of three methods for referring to these fields can be used: specify the field name as the item, specify the field directly, or refer directly to objRecordSet. This is illustrated in the code below. (Never do this in real lifechoose one method for retrieving field names and use it consistently.) Use vbTab to space over the address and phone number lines for ease of reading.
WScript.Echo "Name: " & objRecordset.fields.item("Name")
WScript.Echo vbTab & "Address: " & objRecordset.fields("Address")
WScript.Echo vbTab & "PhoneNumber: " & objRecordset("PhoneNumber")
| 9. | Save and run the script. You should see an output similar to the following:
Name: ed
Address: charlotte
PhoneNumber: 12345
Name: bob
Address: atlanta
PhoneNumber: 456787
Name: sally
Address: Chicago
PhoneNumber: 678345
Name: paul
Address: cincinnati
PhoneNumber: 245987
| 10. | If your script does not produce the expected results, compare your code with \My Documents\Microsoft Press\VBScriptSBS\ch13\QueryTextFileUsingADO.vbs.
|
 |