Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

August 16, 2004 12:00 AM

Scripting and Managed Code Demystified

Complete a simple administrative task in four languages
Windows IT Pro
InstantDoc ID #43317
Rating: (1)
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.

    Related Content:

    ARTICLE TOOLS

    Comments
    • Anonymous User
      7 years ago
      Jan 19, 2005

      You have helped me so much.
      Now I am just waiting on how to make this script more robust.

    You must log on before posting a comment.

    Are you a new visitor? Register Here

    advertisement

    advertisement

    White Papers

    Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

    Essential Guides

    Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

    Web Seminars

    What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

    eLearning Series

    We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
    Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

    Subscribe to Windows IT Pro!

    Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.