Solved

VBScript to connect to a remote AD and search a user

Posted on 2013-11-14
8
2,701 Views
Last Modified: 2013-11-21
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
0
Comment
Question by:vatza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:Raj-GT
ID: 39647476
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.
0
 

Author Comment

by:vatza
ID: 39647617
yes.. thank you..

I could infer that the ADODB connection is open, but the command is closed. Any light in this area ?
0
 
LVL 14

Expert Comment

by:Raj-GT
ID: 39647650
You are using strComp as variable which is invalid. A vbscript function called strComp exists so you have to rename that variable.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:vatza
ID: 39647776
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
0
 
LVL 14

Accepted Solution

by:
Raj-GT earned 500 total points
ID: 39648368
Try this...
strUser = "domain\username"
strPass = "password"
strDC = "DC.domain.local" ' this has to be FQDN of the DC
strAccount = "searchuser" 'Use the sAMAccountname (logon name)  value here instead of CN
            
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)(sAMAccountname=" & 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")
   wscript.echo strDnFound ' Just so that we know it's working
   objRS.MoveNext
Loop 

objRS.close
oConnection.close

Open in new window


Thanks,
Raj
0
 

Author Comment

by:vatza
ID: 39652899
Thanks Raj, It works
0
 
LVL 14

Expert Comment

by:Raj-GT
ID: 39665119
You are welcome. Would you mind closing the question then please.
0
 

Author Comment

by:vatza
ID: 39665286
I am closing it. Thanks
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

728 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