I found this script here from back on 05 and not sure why when converted to a function it does not set the result properly.
Could someone take a look and let me know what is wronge?
Thanks
John
Could someone take a look and let me know what is wronge?
Thanks
John
Code:
strUserOU = GetUserOU(tuser)
WScript.Echo "User OU: " & strUserOU
Function GetUserOU(strUser)
Const ADS_SCOPE_SUBTREE = 2
Dim objConnection: Set objConnection = CreateObject("ADODB.Connection")
Dim objCommand: Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
'Automatically find the domain name
Dim objDomain: Set objDomain = getObject("LDAP://rootDse")
Dim strDomain: strDomain = objDomain.Get("dnsHostName")
Dim strLDAPDomain: strLDAPDomain = "LDAP://" & objDomain.Get("defaultNamingContext")
Dim objLDAPDomain: Set objLDAPDomain = GetObject(strLDAPDomain)
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT distinguishedName FROM '" & strLDAPDomain &_
"'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'"
Set objRecordSet = objCommand.Execute
'objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName").Value
arrPath = Split(strDN, ",")
intLength = Len(arrPath(1))
intNameLength = intLength - 3
strUser = Right(arrPath(1), intNameLength)
GetUserOU = Right(arrPath(1), intNameLength)
objRecordSet.MoveNext
Loop
End Function