我正在尝试从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)
有什么想法吗?
答案 0 :(得分:1)
发现它!!
我需要使用CStr()将objRS(“userAccountControl”)转换为字符串。
这有效:
set uacResult = objRS("userAccountControl")
Response.Write uac.Item(CStr(uacResult))
Response.Write " (" & uacResult & ")"
答案 1 :(得分:1)
您已经找到了valid solution,但我想解释一下这里发生了什么。
Dictionary
对象的键可以是除数组之外的任何类型(请参阅docs)。由于您使用了Set
语句,因此变量uacResult
是一个对象引用(稍后会详细介绍)。当您在Item
上调用Dictionary
方法时,它实际上正在查找与对象引用匹配的键,而不是按预期搜索字符串。 CStr()
通过显式将对象引用转换为字符串来纠正这一点。
由于您在其分配中使用了Set
关键字,uacResult
最终包含对Field
对象的引用,因为Recordset
对象的默认属性是Fields
集合。当您调用CStr(uacResult)
时,这是通过调用Field
对象的默认属性(即Value
属性)将对象转换为字符串。您可以通过几种不同的方式实现相同的效果:
' 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)