• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1046
  • Last Modified:

simple vbs ldap query (windows 2003)

I try to write simple script that query the active directory for current user office number and show it.
I've create connection object and recordset but i dont know how to build exact query.
Can someone help me to build the query or refer me to guide?

Thanks ahead,
  • 2
1 Solution
It's not easy using VBScript to do this.

You can use DSQUERY or DSGET to do this.

If you post a bit more details maybe I can assist.

'To run this script wscript <sciptname.vbs> <username>

Dim sUser, sUserName, sDisplayName, sLine, sDN

Set objArgs=wscript.Arguments

      sDN=UserNameDN(UserName)  'Finds the Distinguished Name of the UserName
      Set oUser = GetObject("LDAP://DCNAME/" & sDN)  'change the path of the DC
      Set objRootDSE = GetObject("LDAP://RootDSE")
      domainDN = objRootDSE.Get("defaultNamingContext")

      wscript.echo domainDN
      wscript.echo number

Public Function UserNameDN(sUserName)
         'On Error Resume next
         Set oRootDSE = GetObject("LDAP://rootDSE")
         Set oConnection = CreateObject("ADODB.Connection")
         oConnection.Open "Provider=ADsDSOObject;"
         Set oCommand = CreateObject("ADODB.Command")
         oCommand.ActiveConnection = oConnection
         oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;(&(objectCategory=User)(samAccountName=" & sUserName & "));distinguishedName;subtree"
         Set oRecordSet = oCommand.Execute
              UserNameDN = oRecordSet.Fields("distinguishedName")
              If Err <> 0 Then
                outFile.WriteLine "There was an error getting the DN for " & strUser & "." & "    " & Err.Num
                strBadCount = strBadCount + 1
              End If
              On Error Goto 0
End Function

omribiAuthor Commented:
star_trek - Many Thanks!!
Where i can find the Schema? i need to get the Office Number/Department .. and not the Telephone Number.

Netman - i know how to do it with DSQUERY, but it must be part of other VBS so i need to implement it with VBS.

so it shld be


To get more info on attributes and what they are called you can check adsiedit.msc which is available from the resource kit
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now