simple vbs ldap query (windows 2003)

Posted on 2006-06-07
Last Modified: 2008-02-01
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,
Question by:omribi
    LVL 51

    Expert Comment

    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.

    LVL 11

    Accepted Solution

    '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


    Author Comment

    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.

    LVL 11

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    So you have two Windows Servers and you have a directory/folder/files on one that you'd like to mirror to the other?  You don't really want to deal with DFS or a 3rd party solution like Doubletake. You can use Robocopy from the Windows Server 200…
    Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now