Subscribe to Windows IT Pro

 

Get Newsletters

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

Subscribe Now!

December 11, 2006 12:00 AM

Using a Logon to Determine a Distinguished Name

Windows IT Pro
InstantDoc ID #94255
Rating: (1)
Downloads
94255.zip

Q: How can I use a logon username to determine a user's distinguished name (DN)?
A: The NameTranslate object can translate a username into a DN that you can use to reference an Active Directory (AD) account. Use the following steps:

  1. Create an instance of the NameTranslate object.
  2. Use the Init method to initialize the Name-Translate object.
  3. Pass the user s name to the NameTranslate object s Set method.
  4. Call the NameTranslate object's Get method to retrieve the user's DN.

Listings 1 and 2, GetDN.vbs and GetDN.js, show a VBScript and JScript version, respectively, of a GetDN function that follows these steps. Callout A in both listings illustrates the first two steps. (For more information about the parameters to the Initmethod, see the Microsoft article IADsNameTranslate::Init at http://msdn2.microsoft.com/en-us/library/aa706046.aspx.)

Next, the GetDN function determines whether the UserName parameter contains a backslash (\) character, as the code at callout B in Listings 1 and 2 shows. If the character doesn't exist (VBScript uses the InStr function, and JScript uses the indexOf method), the function assumes that you're referring to a username in the current domain and retrieves the current domain's name from the User Domain property of the WScript.Network object. The function then prefixes the username with a domain name and trailing backslash (e.g., domain\username). This step is necessary because the NameTranslate object requires the domain\username format.

The GetDN function next follows step 3 and passes the username to the NameTranslate object's Set method. If the username doesn't exist in the current domain, the Set method will fail, so the script needs to handle the error gracefully. VBScript uses the On Error Resume Next statement and JScript uses try ... catch blocks. Callout C in Listings 1 and 2 shows the error-handling process in both languages. The Set method's first parameter specifies the username's format (ADS_NAME_TYPE_NT4 or the value 3 means domain\username format), and the second parameter is the username. If the Set method fails, the function returns an empty string.

Finally, the function completes step 4 and calls the NameTranslate object's Get method, specifying DN format (ADS_NAME_TYPE_1779 or the value 1).

Related Content:

ARTICLE TOOLS

Comments
  • David
    4 years ago
    Aug 26, 2008

    dd

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.