可以通过查询电子邮件地址从活动目录中获取用户名吗?用户名查询电子邮件地址就没问题了:
Set objSysInfo = CreateObject("ADSystemInfo")
Set WshShell = CreateObject("WScript.Shell")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
strEMail = objUser.mail
但在我的情况下,我只有电子邮件,没有用户名。
感谢您的帮助
答案 0 :(得分:0)
我认为你不能直接在LDAP上做这件事。
创建查询并准确提取您所追求的内容( sAMAccountName )会更快:
UserNameFromEmail "Firstname.Lastname@YourDomain.com"
Sub UserNameFromEmail(sEmail)
Const ADS_SCOPE_SUBTREE = 2
Const PageSize = 1000
Dim sRootLDAP, oConnection, oCommand, oRecordSet
sRootLDAP = "'LDAP://" & GetObject("LDAP://RootDSE").Get("defaultNamingContext") & "'"
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
Set oCommand = CreateObject("ADODB.Command")
Set oCommand.ActiveConnection = oConnection
oCommand.CommandText = "Select sAMAccountName from " & sRootLDAP & " Where mail='" & sEmail & "'"
oCommand.Properties("Page Size") = PageSize
oCommand.Properties("Timeout") = 30
oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
oCommand.Properties("Cache Results") = True
Set oRecordSet = oCommand.Execute
oRecordSet.MoveFirst
Do Until oRecordSet.EOF
WScript.Echo "Username for """ & sEmail & """ is """ & oRecordSet.Fields(0) & """"
oRecordSet.MoveNext
Loop
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
End Sub