vbscript从邮件中获取用户名

vbscript从邮件中获取用户名,vbscript,active-directory,ldap,Vbscript,Active Directory,Ldap,是否可以通过查询电子邮件地址从active directory获取用户名?按用户名查询电子邮件地址没有这样的问题: Set objSysInfo = CreateObject("ADSystemInfo") Set WshShell = CreateObject("WScript.Shell") strUser = objSysInfo.UserName Set objUser = GetObject("LDAP://" & strUser) strEMail = objUser.mai

是否可以通过查询电子邮件地址从active directory获取用户名?按用户名查询电子邮件地址没有这样的问题:

Set objSysInfo = CreateObject("ADSystemInfo")
Set WshShell = CreateObject("WScript.Shell")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
strEMail = objUser.mail
但在我的场景中,我只有电子邮件,没有用户名


谢谢你的帮助

我想你不能直接用LDAP来完成

创建查询并准确提取sAMAccountName之后的内容会更快:


也可以进行全文搜索吗?例如,搜索asdf的任何字段?提前感谢您您可以搜索广告对象的所有属性,只是有些属性不是基于文本的,有些是日期、二进制、集合等。
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