vbscript从邮件获取用户名 [英] vbscript get username from mail

查看:91
本文介绍了vbscript从邮件获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过查询电子邮件地址从活动目录中获取用户名?通过用户名查询电子邮件地址,这是没有问题的:

It is possible to get the username from active directory by query the email address ? Query the email address by username its no problem like this:

Set objSysInfo = CreateObject("ADSystemInfo")
Set WshShell = CreateObject("WScript.Shell")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
strEMail = objUser.mail

但是在我的情况下,我只有电子邮件,没有用户名.

But in my scenario I have only the email and no username.

感谢您的帮助

推荐答案

我认为您不能直接从LDAP上删除它.

I don't think you can do it off LDAP directly.

创建查询并准确提取所要查找的内容( sAMAccountName )的速度更快:

It's faster to create a query and extract exactly what you are after (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

这篇关于vbscript从邮件获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆