How to get a computer’s Active Directory OU using VBScript
- 3 Comment
Welcome back!
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: Windows, Scripting, VBScript
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
3 Comments on this post
Trackbacks
-
Umesh said:
Hi,
I am looking for a script which can export computer names from an AD OU to a text file.Any help will be appreciated.
Regards,
UmeshJanuary 21st, 2009 at 1:09 am -
Martin Pullen said:
Thank you for this example, this solution works perfect.
June 24th, 2009 at 4:35 am -
hstagner said:
Hello Martin,
I am glad that I could help! Thank you for reading Searchmarked.com!
June 24th, 2009 at 3:39 pm

