我正在尝试编写一个使用管理员凭据连接到 Active Directory 的脚本。然后在整个域中搜索特定用户名,然后更新该用户的属性。我编写了一个我认为应该可以工作的脚本,但是我收到了“拒绝访问”错误,这很奇怪。
这是脚本,我已将其分成几个部分。因为它应该通过SuperOffice运行,它有自己独特的环境。
Dim strUser, rootDSE, adoConnection, ldapStr, adoRecord, objUser
updateUser()
Public Sub updateUser()
ADUsername = "john.doe"
createADConnection()
If userExistsInAD(ADUsername) = False Then
Exit Sub
End if
objUser.Put "description", "testing"
objUser.SetInfo
End Sub
Public Sub createADConnection()
Set rootDSE = GetObject("LDAP://RootDSE")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADSDSOObject"
adoConnection.Properties("User ID") = "mydomain\administrator"
adoConnection.Properties("Password") = "8g773ggj024g"
adoConnection.Properties("Encrypt Password") = True
adoConnection.Properties("ADSI Flag") = ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION
adoConnection.Open "Active Directory Provider"
End Sub
Public Function userExistsInAD(ByVal strUser)
ldapStr = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">;(&(objectCategory=Person)(objectClass=User)(samAccountName=" & strUser & "));adspath;subtree"
Set adoRecord = adoConnection.Execute(ldapStr)
If Not adoRecord.EOF Then
userExistsInAD = True
Exit Function
End if
userExistsInAD = False
End Function