Solved

VBScript to connect to a remote AD and search a user

Posted on 2013-11-14
8
2,433 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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 to another domain controller. Log onto the new domain controller with a user account t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

809 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