I need a hand with a VBScript I’m writing and know there are a few 'dopers who are VBScript programmers. Basically, I need some code that will determine what Organizational Unit (in Windows 2003 AD) a given user is in. If anyone can give me just a code snippet for it I would really appreciate it.
(I don’t have my own VBScript books with me and it’s really hard to do a search on my iPod)
'Get the default naming context, or set it by hand if you like
Set rootDSE=GetObject(“LDAP://RootDSE”)
DomainContainer = rootDSE.Get(“defaultNamingContext”)
'Set up the AD connection
Set objConn = CreateObject(“ADODB.Connection”)
objConn.Provider = “ADSDSOObject”
objConn.Open “Active Directory Provider”
Set objCommand = CreateObject(“ADODB.Command”)
Set objCommand.ActiveConnection = objConn
'Set the LDAP query, set the sAMAccountName in the string
strQuery = “<LDAP://” & DomainContainer & “>;(sAMAccountName=CHANGETHIS);dn;subtree”
objCommand.CommandText = strQuery
'Execute the LDAP query
Set objRecordSet = objCommand.Execute
'Now do some more stuff that will read the RecordSet and parse the text string, which is the dn of sAMAccountName
'Strip the leading “CN=…,” portion from the string, leaving the OU.