Dec 20 2007

How to get a computer’s Active Directory OU using VBScript

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

Google Query: get computer’s ou with vbscript

I was working on a project for work today where I had to use a script to get a computer’s Active Directory OU and take an action based on the OU that the computer was in. This project will eventually become a logon script. Since our network consists of a mixture of Windows 2000 and Windows XP computers, Powershell was out of the question. It was time to turn to the tried and true VBScript and its fifty-million lines of code to accomplish one small task.

There are a couple of ways that I could query for the computer’s OU using VBScript. I could connect to Active Directory and walk the whole directory tree but this could take a while. I could dump the contents of the Active Directory tree into a text file and search on it, but that is messy. Fortunately, I found the gem of a snippet below at the Tek-Tips Forums. It was posted by PScottC.
Dim wshShell, wshNetwork
Dim strComputerName
' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
' Initialize Variables
strComputerName = wshNetwork.ComputerName
wscript.echo "Computer DN: " & GetDN
Function GetDN()
' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$". Returns comma delimited string to calling code.
Dim objTrans, objDomain
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set objTrans = CreateObject("NameTranslate")
Set objDomain = getObject("LDAP://rootDse")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
& strComputerName & "$"
GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
'Set DN to upper Case
GetDN = UCase(GetDN)
End Function

Basically, this script snippet can be used to convert an NT computer name into a the full Active Directory Distinguished Name for the computer. The following line:
wscript.echo "Computer DN: " & GetDN

Is merely there as a placeholder to show you the output of the “GetDN” function at the end of the script. You can replace it with any code you choose. If you want the script to do something different based on the computer’s OU then the natural choice would be to use the “GetDN” value in an “elseif” or “case” statement. When I have my full script complete using this code snippet, I will share the details. Until then…
Happy Coding!

Technorati Tags: , ,

Related posts:

  1. How to redirect output when using wscript.shell in vbscript
  2. How to deal with “Login Failure: the target account name is incorrect” on a Windows file share
  3. How to redirect users’ favorites folder using VBScript
  4. How to pin a VBScript to the Windows start menu
  5. How to hide typed passwords in a vbscript message box

TAGS:

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                 
                  Computers Blogs - Blog Top Sites