er4z0r
er4z0r

Reputation: 4901

Returning References from Function in VBScript

I am loosing my hair on VBScript. How the heck can I pass a reference as return value of a function?

Currently my code looks like this:

Set objUser = FindUser("bendert")

REM Searches Directory for the User
Function FindUser(UserLoginName)
    Wscript.Echo "Querying AD to retrieve user-data" 

 Set objConnection = CreateObject("ADODB.Connection")
 objConnection.Open "Provider=ADsDSOObject;"

 Set objCommand = CreateObject("ADODB.Command")
 objCommand.ActiveConnection = objConnection

 'Get user Using LDAP/ADO.  There is an easier way
 'to bind to a user object using the WinNT provider,
 'but this way is a better for educational purposes
 Set oRoot = GetObject("LDAP://rootDSE")
 'work in the default domain
 sDomain = oRoot.Get("defaultNamingContext")
 Set oDomain = GetObject("LDAP://" & sDomain)
 sBase = "<" & oDomain.ADsPath & ">"
 'Only get data for login name requested
 sFilter = "(&(sAMAccountName="& UserLoginName &")(objectClass=user))"
 sAttribs = "adsPath"
 sDepth = "subTree"

 sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
 WScript.Echo "LDAP Query is:" & sQuery &""

 objCommand.CommandText=sQuery
 Set objRecordSet = objCommand.Execute

 FindUser = GetObject(objRecordSet.Fields("adspath"))
 WScript.Echo "You E-Mail Address is: " & objUser.EmailAddress
 objConnection.Close    
End Function

Unfortunatley VBScript throws an error on the line where I make an assignment to the function's return value.

FindUser = GetObject(objRecordSet.Fields("adspath"))

The Error looks like "wrong number of arguments or invalid property assignment".

What am I doing wrong?

Upvotes: 3

Views: 1382

Answers (2)

dmb
dmb

Reputation: 603

Looks like you need:

Set FindUser = GetObject(objRecordSet.Fields("adspath"))

Upvotes: 4

Related Questions