1

我正在尝试从 Active Directory 中查询 userAccountControl 并将其与我在脚本中设置的字典进行匹配。

首先,我设置我的字典对象并填充它:

dim uac
Set uac=Server.CreateObject("Scripting.Dictionary")
uac.add "512", "Enabled Account"
uac.add "514", "Disabled Account"
uac.add "544", "Enabled, Password Not Required"
uac.add "546", "Disabled, Password Not Required"
uac.add "66048", "Enabled, Password Doesn't Expire"
uac.add "66050", "Disabled, Password Doesn't Expire"
uac.add "66080", "Enabled, Password Doesn't Expire & Not Required"
uac.add "66082", "Disabled, Password Doesn't Expire & Not Required"
uac.add "262656", "Enabled, Smartcard Required"
uac.add "262658", "Disabled, Smartcard Required"
uac.add "262688", "Enabled, Smartcard Required, Password Not Required"
uac.add "262690", "Disabled, Smartcard Required, Password Not Required"
uac.add "328192", "Enabled, Smartcard Required, Password Doesn't Expire"
uac.add "328194", "Disabled, Smartcard Required, Password Doesn't Expire"
uac.add "328224", "Enabled, Smartcard Required, Password Doesn't Expire & Not Required"
uac.add "328226", "Disabled, Smartcard Required, Password Doesn't Expire & Not Required"

然后我连接到我的 Active Directory 来查询它:

Set objDomain = GetObject ("GC://RootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.provider ="ADsDSOObject"
objConn.Properties("User ID") = "domain\administrator"
objConn.Properties("Password") = "password"
objConn.Properties("Encrypt Password") = True
objConn.open "Active Directory Provider"
Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objConn
objCom.CommandText ="select name,userAccountControl FROM 'GC://"+objADsPath+"' where sAMAccountname='*' and objectCategory='user' and objectCategory='person' ORDER by sAMAccountname"

然后我像这样遍历结果:

Set objRS = objCom.Execute
Do While Not objRS.EOF Or objRS.BOF
Response.Write objRS("name")
set uacResult = objRS("userAccountControl")
objRS.MoveNext
Response.Flush
Loop
objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing
Set objCom = Nothing
Set objADsPath = Nothing
Set objDomain = Nothing

这一切都很好。我现在要做的是查询我使用来自 objRS("userAccountControl") 的结果创建的 uac 字典。当我尝试这样做时,它返回一个空白值?

set uacResult = objRS("userAccountControl")
Response.Write uac.Item(uacResult)
Response.Write " (" & uacResult & ")"

我很难过 - objRS 值是作为字符串返回的吗?如果我执行 Response.Write uac.Item("512") 它可以工作,但如果我执行 Response.Write uac.Item(uacResult) 则不行

有任何想法吗?

4

2 回答 2

1

找到了!!

我需要使用 CStr() 将 objRS("userAccountControl") 转换为字符串。

这有效:

set uacResult = objRS("userAccountControl")
Response.Write uac.Item(CStr(uacResult))
Response.Write " (" & uacResult & ")"
于 2013-07-22T11:22:51.437 回答
1

您已经找到了一个有效的解决方案,但我想解释一下这里发生了什么。

对象的键Dictionary可以是除数组之外的任何类型(请参阅文档)。由于您使用了该Set语句,因此该变量uacResult是一个对象引用(稍后会详细介绍)。当您在 上调用该Item方法时Dictionary,它实际上是在寻找与对象引用匹配的键,而不是像您预期的那样搜索字符串。CStr()通过将对象引用显式转换为字符串来纠正此问题。

由于您Set在其赋值中使用了关键字,因此uacResult最终包含对Field对象的引用,因为对象的默认属性RecordsetFields集合。当您调用 时,这是通过调用对象CStr(uacResult)的默认属性(即属性)将对象转换为字符串。您可以通过几种不同的方式实现相同的效果:FieldValue

' method 1 - from accepted answer (https://stackoverflow.com/a/17786308/249624)
set uacResult = objRS("userAccountControl")
Response.Write uac.Item(CStr(uacResult))

' method 2 - explicitly using the Value property
set uacResult = objRS("userAccountControl")
Response.Write uac.Item(uacResult.Value)

' method 3 - make uacResult a string instead of a Field object
uacResult = objRS("userAccountControl")
Response.Write uac.Item(uacResult)

' method 4 - same as method 3, but explicitly use the Value property
uacResult = objRS("userAccountControl").Value
Response.Write uac.Item(uacResult)
于 2013-07-23T07:33:48.660 回答