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.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
  if not rs.BOF and not rs.EOF then
        FindUser = rs(0)
   FindUser = “Not Found”
  end if
 end if
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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s