I'm trying to get authentication with AD and to do this I've already made this code. Can anyone provide me if it's correct or not to do this or if I have a better solution.
On Error Resume Next
Set obj = GetObject("ldap://serverip/DC=xx, DC=xx")
WScript.Echo "A: " & obj.name
WScript.Echo "init script"
strUser = "TESTAD\Administrador"
strPass = "test"
strDC = "serverip" ' this has to be FQDN of the DC
strAccount = "Administrador" '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=administrador,cn=Users,dc=xx, dc=xx"
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 "found it!"
wscript.echo "query: " & strQuery
wscript.echo strTargetDncDomain
wscript.echo strDnFound ' Just so that we know it's working
objRS.MoveNext
Loop
objRS.close
oConnection.close
It's they first time doing this so i was looking for some help. And also after this I want to save the results into a txt file, it's correct to use
cscript.exe //NoLogo "C:\path\to\your.vbs" >"C:\output.txt"