VBScript to find LDAP Path of the user

on Error resume Next

username = Inputbox(“Whats the username”)

 set objRoot = getobject(“LDAP://RootDSE“)
 domainname = objRoot.get(“defaultNamingContext”)
 wscript.echo  finduser(username,domainname)

 
Function FindUser(Byval UserName, Byval Domain)
 on error resume next

 set cn = createobject(“ADODB.Connection”)
 set cmd = createobject(“ADODB.Command”)
 set rs = createobject(“ADODB.Recordset”)

 cn.open “Provider=ADsDSOObject;”
 
 cmd.activeconnection=cn
 cmd.commandtext=”SELECT ADsPath FROM ‘LDAP://” & Domain & _
    “‘ WHERE sAMAccountName = ‘” & UserName & “‘”
 
 set rs = cmd.execute

 if err<>0 then
  FindUser=”Error connecting to Active Directory Database:” & err.description
 else
  if not rs.BOF and not rs.EOF then
        rs.MoveFirst
        FindUser = rs(0)
  else
   FindUser = “Not Found”
  end if
 end if
 cn.close
end function

2 thoughts on “VBScript to find LDAP Path of the user

  1. Just wanted to say a massive thank you!
    Have been looking for this everywhere!

    how can you convert finduser(username,domainname) into a string that can be stored in a varible. eg strADSPath

Leave a comment