从VBScript中的函数返回引用

时间:2010-04-08 15:22:13

标签: windows scripting vbscript

我在VBScript上失去了我的头发。如何将引用作为函数的返回值传递?

目前我的代码如下:

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在我为函数的返回值赋值的行上抛出一个错误。

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

错误看起来像“错误的参数数量或无效的属性赋值”。

我做错了什么?

2 个答案:

答案 0 :(得分:3)

看起来你需要:

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

答案 1 :(得分:1)