Downloads
43317.zip

I'm always amazed to observe how a passion for technology often morphs into a religion. An example of this phenomenon is the various Perl and VBScript scripting zealots. Each language has its place, as do programming languages such as C# and Visual Basic .NET. However, drawing lines between these languages is pointless and nonproductive.

Rather than point out differences, I want to show you how similar the code can be among Perl, VBScript, C#, and Visual Basic .NET. To do so, I'll show you how to complete a simple but important task—creating an Active Directory (AD) user account— in all four of those languages. (I've opted not to show you how to display Hello World on the console in all four languages—a simple procedure but one of the most overscrutinized programming tasks.)

Defining Terms
Regardless of the language you choose, creating an AD object—such as a user account—involves the following four steps:

  1. Connect to the AD container in which you want to create the object.
  2. Create the object.
  3. Set any mandatory attributes that AD doesn't set automatically.
  4. Save the object to AD.

You might be asking, "If that's all there is to it, why have I seen code examples for creating a user account that contain 300 or more lines of code?" You need to be precise about the type of code examples. Generally, you'll encounter two types: those that are instructive and those that show complete solutions.

There's a big difference between an instructive code example and a complete code solution. A complete code solution for creating user accounts will contain error handling (e.g., to check for an existing user account before creating a new one), console output, and perhaps other procedures. For example, a complete code solution might read from a spreadsheet containing user account information, then use that information to generate user accounts. In contrast, an instructive code example will show only the essential steps necessary for completing a task. An instructive code example for creating an AD user account will therefore contain only four or five lines of code. Of course, this distinction between a code solution and a code example isn't limited to AD user account creation. You can reduce almost any systems administration task to a few lines of code, regardless of the language in which you write it.

Choosing Languages
To demonstrate the task of AD user account creation, I chose two scripting languages (VBScript and Perl) and two programming languages (Visual Basic .NET and C#). VBScript is arguably the easiest language to understand, and it's a language that Microsoft fully supports for Windows systems administration. Perl is a natural choice because it's a pervasive cross-platform scripting language that many Windows administrators and UNIX and Linux administrators use. I chose Visual Basic .NET and C# because they're both included with the Windows .NET Framework and are the most popular languages in the Framework. In addition, if you're familiar with Visual Basic (VB) or VBScript, Visual Basic .NET is easy to understand. If you're familiar with JScript or Java, C# is easy to follow.

Putting It Simply
If you've never coded in any language before, don't despair. You'll be surprised by how easily you can program in any of these languages when you isolate a specific task from all the other stuff that surrounds the code. Some people might argue that this is an oversimplification of scripting and development in general. However, the key to working with any technology discipline, programming or otherwise, is starting with simple examples that demonstrate the completion of a basic task. After you see how to apply a technology, you're better prepared to dig into the intricacies of a robust and powerful discipline such as programming. The code in Listing 1, Listing 2, Listing 3, and Listing 4 performs the following steps to create a user account:

  1. Connects to the TechWriters organizational unit (OU) in the fabrikam. com domain.
  2. Creates a user account named Ken Myer.
  3. Sets the sAMAccountName mandatory attribute to MyerKen.
  4. Saves the user account to the TechWriters OU.

But don't try to run the code yet! You must complete some preliminary tasks. First, you need to configure your client and server environment in a specific way to run these examples. Second, you must verify that the underlying client infrastructure can run the code. Third, except for VBScript, you must add a line or two of code to supplement the actual task code. Let's walk through each step.

Configuring the Client and Server
To get started, you need to ensure that your computer (the client) is a member of an AD domain named fabrikam .com with an OU named TechWriters. Also, a user account with a sAMAccountName of MyerKen must not exist in the domain, and the Tech- Writers OU must not contain a user account with a CN value equal to Ken Myer.

I could have made the examples more dynamic, but doing so would have added complexity to the code examples, thus defeating the primary purpose of this article—to create instructional code examples. That being said, you can modify the task code that appears in the listings to point to a different domain or to create a user account with a different name. Here are some examples of changes you can make:

  • In line 1, change ou=TechWriters, dc=fabrikam,dc=com to ou=HR,dc= adatum,dc=com to connect to the HR OU in the adatum.com domain.
  • In line 2, change CN=Ken Myer to CN=John Doe to use the name John Doe instead of Ken Myer.
  • In line 3, change MyerKen to Doe- John to specify a sAMAccountName of DoeJohn instead of MyerKen.
  • Preparing the Scripting Environment
    The scripting languages require a scripting engine, language interpreters, and COM objects to complete tasks. Windows Script Host (WSH) is the Windows scripting engine that lets you run scripts. Even though Windows 2000 and later include WSH by default, you should upgrade to WSH 5.6 for all Windows platforms except Windows Server 2003. Windows 2003 comes with the most recent build of WSH 5.6. You can find WSH 5.6 at http:// msdn.microsoft.com/scripting.

    WSH can run VBScript code out of the box because WSH includes the VBScript language interpreter. In contrast, Microsoft doesn't provide a Perl interpreter in WSH, but you can easily install one. Go to http://www.active state.com/products/download/down load.plex?id=activeperl to download and install a Windows Installer (MSI) file of ActivePerl 5.8.1 or later. After you install the Perl interpreter, you can run Perl scripts in WSH.

    The COM objects for administering AD from scripts are contained in Active Directory Service Interfaces (ADSI). Win2K and later include ADSI. For Windows NT 4.0 Service Pack 6a (SP6a) and Windows 9x users, Microsoft has created the AD Client Extensions. The Win9x-based client extensions are contained on the Win2K installation CD-ROM. You can download the AD Client Extensions for NT Workstation 4.0 from http:// www.microsoft.com/ntworkstation/ downloads/other/adclient.asp.

    Preparing the Managed Code Environment
    The managed code languages (Visual Basic .NET and C#, in these examples) require Microsoft .NET classes to complete tasks. Windows 2003 and later include the .NET classes. For most other Windows OSs, you can download .NET Framework 1.1, which contains the .NET classes and other important tools, from Microsoft Windows Update. When you check for updates from a computer that doesn't have .NET Framework 1.1, .NET Framework 1.1 appears at the Microsoft Windows Update site in the Pick updates to install menu, underWindows or Windows XP, depending on the OS from which you run the update.

    .NET Framework 1.1 operates on Windows 2003, Windows XP, Win2K, Windows Me, and Win98 computers. You must also be running Microsoft Internet Explorer (IE) 5.01 or later. If you're unsure whether you're running .NET Framework 1.1, go to the Control Panel Add/Remove Programs applet and look for a Microsoft .NET Framework 1.1 entry. You can also search for the \%windir%\Microsoft.NET\Frameworkv1.1.4322 path. The folder name's 4322 designation is the build number of version 1.1 of the Framework and is the current build number as of December 2003. To learn more about the Framework, check out http://msdn.microsoft.com/net framework.

    Compiling Code
    WSH interprets scripts at runtime, which means you don't have to compile them to run. However, you must compile managed code before it will run. For large, complex code, compiled programs run faster than interpreted code and compiled code is inherently more secure. There's a lot more to understand about interpreted and compiled code, but for the purpose of this article, all you need to know is that you must compile managed code before you run it.

    The Framework includes two important tools for compiling the managed code examples. For Visual Basic .NET, the name of the command- line compiler is vbc.exe. For C#, it's csc.exe. Both of these applications reside in the Framework parent directory. In the case of .NET Framework 1.1, this directory is the v1.1.4322 folder. For the sake of convenience, I recommend that you add the \%windir%\Microsoft.NET\Frameworkv1.1.4322 path to your Path environment variable so that you don't have to specify the entire path to run the compilers. (Replace %windir% with the location of your Windows directory.) Of course, if you have Visual Studio .NET installed, you won't need to navigate to the parent folder that contains the Framework or add a path specification to your Path environment variable to compile managed code. Instead, you can compile your code by using the Visual Studio .NET command prompt or the Visual Studio .NET IDE.

    Before you compile the C# and Visual Basic .NET examples, you need to add a few more lines of code. In the next section, I describe those additions, then provide you with the compiler command and parameters you need to compile the sample code.

    Preparing the Examples
    Each piece of sample code requires some minor preparation before it's ready to run. For the VBScript example, copy the code in Listing 1 to a text file, then save the file as listing1.vbs. You can save all code examples to the same folder.

    For the Perl example, you must add use Win32::OLE to the top of the file, as Listing 5, shows. Win32::OLE is the name of a Perl module that provides, among other things, the ability to use Windows' COM-based libraries¡ª specifically, for this example, the COMbased libraries in ADSI. Copy the code in Listing 5 to a text file, then save the file with a .pl extension.

    For the Visual Basic .NET and C# examples, you must add a few lines of code to the beginning and end of the code, as Listing 6 and Listing 7 show. These additional code lines are required but aren't specific to the examples. The sidebar "The Visual Basic .NET and C# Surrounding Code" provides more information about these additional lines of code.

    To compile the Visual Basic .NET example, copy the code in Listing 6 to a text file, then save the file as listing6. vb. From the command prompt, navigate to the file folder and run the command

    vbc /t:exe /r:system.dll
    /r:system.directoryservies
    .dll listing6.vb

    to create a program named listing6 .exe. The Vbc command-line tool, part of the Framework, lets you compile source code written in Visual Basic .NET into an application from the command line. The /r switches tell the Vbc program which DLLs contain classes required to run the application. These DLLs are included with the Framework. The listing6.vb filename is the name of the file that contains the source code the command.

    To compile the C# example, copy the code in Listing 7 to a text file, then save the file as listing7.cs. From the command prompt, navigate to the file folder and run the command

    csc /t:exe /r:system
    .directoryservices.dll
    listing7.cs

    to create a program named listing7 .exe. In this case, the Framework's Csc command-line tool lets you compile source code written in C# into an application. The switches work similarly to the Vbc example for compiling Visual Basic .NET code from the command line. However, you don't need to specify the system.dll class when compiling the C# example.

    Running the Examples
    For consistency, each code example uses the same OU, the same CN (Ken Myer), and the same sAMAcccount- Name (MyerKen). Therefore, after running each code sample successfully, open the Microsoft Management Console (MMC) AD Users and Computers snap-in and delete the Ken Myer user account in the TechWriters OU prior to running the next code sample. To run each sample, double-click the script file or compiled code. Alternatively, go to the command prompt, navigate to the file folder, type the name of the file, and press Enter.

    Are You Encouraged?
    You now know how to create an AD user account in four different coding languages. The key is stripping a task down to its core and working from there. Each language presents a surprisingly similar approach to coding the task.

    In upcoming articles, I'll augment this task so that you can specify user account information from the command line, create multiple user accounts, and handle errors. Beyond that, my goal is to continue to enlighten you about the relative ease with which you can complete other crucial systems administration tasks from all of these languages.