Find Users E-Mail via SID using VBScript and ADSI

Posted by er4z0r on Stack Overflow See other posts from Stack Overflow or by er4z0r
Published on 2010-03-09T10:34:55Z Indexed on 2010/03/09 10:36 UTC
Read the original article Hit count: 425

Hi,

I am parsing log messages about changes to user accounts on a windows system. I want to notify the user about the changes so I need to retrieve their personal information (First,Last, E-Mail) from Active Directory.

I already found a way to retrieve the username but that is only via WMI and not ADSI:

Function FindUser(Message)
    Dim objWMIService
    Dim strAccountRegex
    Dim objRegex
    Dim objMatch
    Dim strComputer
    Dim objUser
    Dim objShell


    strAccountRegex = "(\%\{[A-Z,0-9,\-]*\})"
    strComputer = "."

    Wscript.StdOut.writeLine "Querying WMI to retrieve user-data" 

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set objShell    = WScript.CreateObject("WScript.Shell")
    Set objRegex    = new RegExp
    objRegex.Pattern= strAccountRegex
    for each objMatch in objRegex.Execute(Message)
            REM Wscript.StdOut.writeLine "Found an Account ID: " & objMatch.value
            Dim strSID
            strSID=NormalizeSID(objMatch.value)
            REM Wscript.Echo "SID after escaping: " & strSID
            Set objUser = objWMIService.Get _
            ("Win32_SID.SID='" & strSID & "'")
    next
    FindUser=objUser.ReferencedDomainName & "\" & objUser.AccountName
End Function

It works fine, but I would like to do it via Active Directory instead of going via WMI. Can you help me?

© Stack Overflow or respective owner

Related posts about vbscript

Related posts about adsi