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("defaultNam ingContext ")
strBase = "<" & "LDAP://" & strComp & "/" & strTargetDncDomain & ">;"
strAttrs = "distinguishedName,sAMAcco untName;"
strScope = "subtree"
strFilter = "(&(objectCategory=person) (objectCla ss=user)(C N=" & strAccount & "));"
strQuery = strBase & strFilter & strAttrs & strScope
Set oConnection = CreateObject("ADODB.Connec tion")
oConnection.Provider = "ADsDSOObject"
oConnection.Properties("En crypt Password") = True
oConnection.Open "Active Directory Provider", strUser, strPass
Set oCommand = CreateObject("ADODB.Comman d")
oCommand.ActiveConnection = oConnection
oCommand.Properties("Page Size") = 100
oCommand.Properties("Timeo ut") = 30
oCommand.Properties("Cache Results") = False
oCommand.CommandText = strQuery
Set objRS = oCommand.Execute
Do While Not objRS.EOF
strDnFound = objRS.Fields("distinguishe dName")
objRS.MoveNext
Loop
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:
strTargetDncDomain = objRootDse.Get("defaultNam
strBase = "<" & "LDAP://" & strComp & "/" & strTargetDncDomain & ">;"
strAttrs = "distinguishedName,sAMAcco
strScope = "subtree"
strFilter = "(&(objectCategory=person)
strQuery = strBase & strFilter & strAttrs & strScope
Set oConnection = CreateObject("ADODB.Connec
oConnection.Provider = "ADsDSOObject"
oConnection.Properties("En
oConnection.Open "Active Directory Provider", strUser, strPass
Set oCommand = CreateObject("ADODB.Comman
oCommand.ActiveConnection = oConnection
oCommand.Properties("Page Size") = 100
oCommand.Properties("Timeo
oCommand.Properties("Cache
oCommand.CommandText = strQuery
Set objRS = oCommand.Execute
Do While Not objRS.EOF
strDnFound = objRS.Fields("distinguishe
objRS.MoveNext
Loop
ASKER
yes.. thank you..
I could infer that the ADODB connection is open, but the command is closed. Any light in this area ?
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.
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("defaultNam ingContext ")
strBase = "<" & "LDAP://" & strDC & "/" & strTargetDncDomain & ">;"
strAttrs = "CN;"
strScope = "subtree"
strFilter = "(&(objectCategory=person) (objectCla ss=user)(C N=" & strAccount & "));"
strQuery = strBase & strFilter & strAttrs & strScope
Set oConnection = CreateObject("ADODB.Connec tion")
oConnection.Provider = "ADsDSOObject"
oConnection.Properties("En crypt Password") = True
oConnection.Open "Active Directory Provider", strUser, strPass
Set oCommand = CreateObject("ADODB.Comman d")
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
'''' 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:
strTargetDncDomain = objRootDse.Get("defaultNam
strBase = "<" & "LDAP://" & strDC & "/" & strTargetDncDomain & ">;"
strAttrs = "CN;"
strScope = "subtree"
strFilter = "(&(objectCategory=person)
strQuery = strBase & strFilter & strAttrs & strScope
Set oConnection = CreateObject("ADODB.Connec
oConnection.Provider = "ADsDSOObject"
oConnection.Properties("En
oConnection.Open "Active Directory Provider", strUser, strPass
Set oCommand = CreateObject("ADODB.Comman
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Raj, It works
You are welcome. Would you mind closing the question then please.
ASKER
I am closing it. Thanks
Set objRootDse = objDSO.OpenDSObject("LDAP:
Are you getting any other error messages.