Using Visual Basic and VBScript

Visual Basic is a powerful interactive environment that can be used for developing both applications and scripts. You can first design and debug Visual Basic applications, and then transform them into VBScript scripts. These language versions have some differences, but they can be easily bypassed.

You need to add references to the appropriate Active Directory libraries to Visual Basic projects. Click References in the Project menu and check the Active DS Type Library box in the Available References list. If you use the ADSI Resource Kit, you also need to add the appropriate DLLs (such as ADsSecurity.dll or ADsError.dll) to the project's references. (Don't forget to register these DLLs: enter regsvr32 <DLLName>.dll at the command prompt.) To use ADSI search via ADO, add the Microsoft ActiveX Data Objects Library to the references as well.

The standard Visual Basic feature — Code Completion Assistant — significantly simplifies ADSI for beginners. You need not remember all of the methods and properties of an ADSI interface, since the assistant will display them for each declared variable (Fig. 16.1).

click to expand
Fig. 16.1: Code Completion Assistant will help you to correctly select a method according to the object's definition

There is, however, a pitfall here. You remember that an ADSI object can implement several interfaces at once, so you can call methods of all these interfaces and access all their properties. Code Completion Assistant, however, only displays information about the interface that was used in a variable declaration. Therefore, the other interfaces will be accessible, but not "visible".

In Visual Basic, you should carefully select variable declarations. Consider, for example, the following program that enumerates all child objects in an OU:

     Dim ADsPath As String     Dim objContainer As IADsContainer     Dim objChild As IADs     ADsPath = "LDAP://OU=Staff, DC=net, DC=dom"     Set objcontainer = GetObject(ADsPath)     For Each objChild In objContainer        Debug.Print objChild.Name + " ---<" + objChild.Class     Next 

Suppose you wish to view all users in this OU and use the following declaration

     Dim objChild As IADsUser 

instead of

     Dim objChild As IADs 

The program will work fine if the OU contains user objects only. However, you will get the error "Type mismatch" if there are objects of other types in this OU. In this case you must use a more "universal" interface — IADs. VBScript would not impose such a problem, since all its variables always have one fundamental data type, Variant.

Converting a Visual Basic Program to a Script

To transform a Visual Basic program into a script in VBScript, it is sufficient to perform the following operations over the program code:

  • Delete the Sub and End Sub statements

  • In variable declarations (the Dim statements), delete all substrings, beginning with (and including) the As keyword

  • Replace the Debug.Print statement (or a similar one) with WScript.Echo

Certainly, a reverse conversion is also possible.

Debugging Scripts

The simplest tool for debugging scripts written on VBScript or JScript is the Microsoft Script Debugger. You can download Script Debugger from the Microsoft website (see Appendix A). To start a debug session, use a command similar to the following (the script name must include the extension):

    cscript Enumerating.vbs //X 

The debugger starts and the script code is displayed in the debugger window. Then you can perform the following operations.

  1. Start the script by pressing the <F5> key.

  2. Point to the code line(s), toggle breakpoint(s) (<F9> key), and run the code between them.

  3. View and modify the values of variables. Select the Command Window from the View menu.

  4. Stop debugging. The debugging session terminates, and you can only save the modified text or immediately exit the debugger.


You cannot actually change the script code in the debugger window. All changes to the code require that you restart the debug session.

A sample debugging session is shown in Fig. 16.2. The presenting code is used for enumerating objects in a specified container, i.e., the code has a loop. Let us discuss some details of working with the command window.

click to expand
Fig. 16.2: Viewing the current values of variables in a script debugging session

The first request for a value for the object (objContainer) was unsuccessful in this example, since the debugger cannot display the entire object. Step 1 shows that we can view the current value of any valid property of the specified object. In successive loops, we can trace the same property or select others (Step N). Note that the character case in the names of objects and properties does not matter. It is possible at any moment to view and modify all objects already created. For instance, while debugging the presented code, we can use the ? objContainer.Parent command and get the parent name of the specified container.

To change a value, enter an assignment statement in the command window. In our example, we could enter the following string after executing the first line of the code:

    ADsPath = "LDAP://DC=net, DC=dom" 

After this modification, we will get a list of child objects that relate to the domain container rather than to the Staff OU, as was initially specified.

Certainly, debugging in such an environment as Visual Basic offers many more possibilities and features in comparison to the Script Debugger.

ADSI Error Codes

You have two options for interpreting error in ADSI scripts or applications.

  • Find "Win32 Error Codes for ADSI 2.5" in ADSI SDK or MSDN (see links in Appendix A).

  • Use the repadmin /shomsg <errCode> command (see detailed information in Chapter 11, "Verifying Network and Distributed Services").

Windows  .NET Domains & Active Directory
Windows .NET Server 2003 Domains & Active Directory
ISBN: 1931769001
EAN: 2147483647
Year: 2002
Pages: 154 © 2008-2017.
If you may any questions please contact us: