Link to home
Start Free TrialLog in
Avatar of vatza
vatza

asked on

VBScript to connect to a remote AD and search a user

I am a newbie to vbscript and looking for a script to bind to a remote AD. I am not sure what am I missing from the below script that I have. Any help is appreciated.

                strUser = "<<BIND USER>>"
            strPass = "**********"
            strComp = "****************" '* the remote domain controller
            strAccount = "<<Search User Account>>"
            
            Const ADS_SECURE_AUTHENTICATION = 1
            Const ADS_SERVER_BIND = &h0200

            Set objDSO = GetObject("LDAP:")
            Set objRootDse = objDSO.OpenDSObject("LDAP:/" & strComp & "/RootDSE" , strUser, strPass, ADS_SECURE_AUTHENTICATION OR ADS_SERVER_BIND)
            strTargetDncDomain = objRootDse.Get("defaultNamingContext")


            
            strBase    = "<" & "LDAP://" & strComp & "/" & strTargetDncDomain & ">;"
            strAttrs = "distinguishedName,sAMAccountName;"
            strScope = "subtree"
            strFilter = "(&(objectCategory=person)(objectClass=user)(CN=" & strAccount & "));"

            strQuery = strBase & strFilter & strAttrs & strScope

            Set oConnection = CreateObject("ADODB.Connection")
            oConnection.Provider = "ADsDSOObject"
            oConnection.Properties("Encrypt Password") = True
            oConnection.Open "Active Directory Provider", strUser, strPass

            Set oCommand = CreateObject("ADODB.Command")
            oCommand.ActiveConnection = oConnection
            oCommand.Properties("Page Size") = 100
            oCommand.Properties("Timeout") = 30
            oCommand.Properties("Cache Results") = False

            oCommand.CommandText = strQuery
            Set objRS = oCommand.Execute
            Do While Not objRS.EOF
                  strDnFound = objRS.Fields("distinguishedName")
                  objRS.MoveNext
            Loop
Avatar of Raj-GT
Raj-GT
Flag of United Kingdom of Great Britain and Northern Ireland image

You are missing a / next to LDAP:/ in the line below. The code looks ok otherwise.

Set objRootDse = objDSO.OpenDSObject("LDAP:/" & strComp & "/RootDSE" , strUser, strPass, ADS_SECURE_AUTHENTICATION OR ADS_SERVER_BIND)

Are you getting any other error messages.
Avatar of vatza
vatza

ASKER

yes.. thank you..

I could infer that the ADODB connection is open, but the command is closed. Any light in this area ?
You are using strComp as variable which is invalid. A vbscript function called strComp exists so you have to rename that variable.
Avatar of vatza

ASKER

I have renamed it and the script executes successfully, but for that error. I infered it with  a msgbox(). Below is the updated Script:

'''' CUSTOM CODE STARTS '''''''
 
                                strUser = "<<BIND USER>>"
                                strPass = "***********"
                                strDC = "***************" '* the remote domain controller
                                strAccount = "<<SEARCH USER>>"
                               
                                Const ADS_SECURE_AUTHENTICATION = 0
                                Const ADS_SERVER_BIND = 389

                                Set objDSO = GetObject("LDAP:")
                                Set objRootDse = objDSO.OpenDSObject("LDAP://" & strDC & "/RootDSE" , strUser, strPass, ADS_SECURE_AUTHENTICATION OR ADS_SERVER_BIND)
                                strTargetDncDomain = objRootDse.Get("defaultNamingContext")
               
                                strBase    = "<" & "LDAP://" & strDC & "/" & strTargetDncDomain & ">;"
                                strAttrs = "CN;"
                                strScope = "subtree"
                                strFilter = "(&(objectCategory=person)(objectClass=user)(CN=" & strAccount & "));"

                                strQuery = strBase & strFilter & strAttrs & strScope

                                Set oConnection = CreateObject("ADODB.Connection")
                                oConnection.Provider = "ADsDSOObject"
                                oConnection.Properties("Encrypt Password") = True
                                oConnection.Open "Active Directory Provider", strUser, strPass

                                Set oCommand = CreateObject("ADODB.Command")
                                oCommand.ActiveConnection = oConnection
                                oCommand.CommandTimeout = 30

                                oCommand.CommandText = strQuery

                                Set objRS = oCommand.Execute


                                Do While Not objRS.EOF
                                                strDnFound = objRS.Fields("CN")
                                                objRS.MoveNext
                                Loop
                               
                                objRS.close
                                oConnection.close
ASKER CERTIFIED SOLUTION
Avatar of Raj-GT
Raj-GT
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vatza

ASKER

Thanks Raj, It works
You are welcome. Would you mind closing the question then please.
Avatar of vatza

ASKER

I am closing it. Thanks