Solved

VBScript to connect to a remote AD and search a user

Posted on 2013-11-14
8
2,107 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
  • 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Synchronize a new Active Directory domain with an existing Office 365 tenant
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…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now