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=ADs开发者_运维问答DSOObject;"
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?
Looks like you need:
Set FindUser = GetObject(objRecordSet.Fields("adspath"))
Set FindUser = ...
http://msdn.microsoft.com/en-us/library/4afksd44%28VS.85%29.aspx
精彩评论