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

simple vbs ldap query (windows 2003)

Hi,
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,
Omri.
0
omribi
Asked:
omribi
  • 2
1 Solution
 
Netman66Commented:
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.

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



Dim sUser, sUserName, sDisplayName, sLine, sDN


Set objArgs=wscript.Arguments
UserName=objArgs(0)

      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")

      'domainDN=oUser.Get("defaultNamingContext")
      wscript.echo domainDN
      number=ouser.telephonenumber
      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
              Err.Clear
              On Error Goto 0
End Function

0
 
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.

Thanks.
0
 
star_trekCommented:
so it shld be

dept=ouser.department
office=ouser.physicaldelivaryofficename

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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