English 中文(简体)
Findusers E-mail through SID using VB texts andactive Directory
原标题:Find Users E-Mail via SID using VBScript and Active Directory

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.

我已经找到了检索用户名称的途径,但这只是通过世界海事委员会,而不是通过自动识别系统:

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 & "
ootcimv2")
    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?

最佳回答

OK. I found a way to do this via Active Directory. For compeleteness here is the code:

REM Converts the SID into a from, that can be processed by WMI
Function NormalizeSid(strSidToNormalize)
  Dim regEx,strReplace
  strReplace=""
    Create regular expression.
  Set regEx = New RegExp
  regEx.Global  = True
  regEx.Pattern = "(%|{|})"
  regEx.IgnoreCase = True

    Make replacement.
  NormalizeSid = regEx.Replace(strSidToNormalize, strReplace)
End Function

REM Searches for a SID the in the Message that was passed as argument
REM SID returned will be of the  form %{S-1-5-21-3968247570-3627839482-368725868-1110}
REM NOTE: Neither WMI nor ADSI will accept this. Use NormalizeSid like in FindUser
Function FindSidInMessage(Message)
    Dim strAccountRegex
    Dim objRegex
    Dim objMatch
    Dim strSID

    strAccountRegex = "(\%{S-[,0-9,-]*})"
    Set objRegex    = new RegExp
    objRegex.Pattern= strAccountRegex

    for each objMatch in objRegex.Execute(Message)
            REM Wscript.StdOut.writeLine "Found an Account ID: " & objMatch.value
            strSID=objMatch.value
    next

    FindSidInMessage=strSID
End Function 

REM Searches Directory for the User matching the SID passed as parameter
Function FindUser(userSID)
    Dim normalizedSID
    Dim objUser

    normalizedSID=NormalizeSid(userSID)
    Wscript.Echo "SID after escaping: " & normalizedSID

    Wscript.StdOut.writeLine "Querying AD to retrieve user-data" 
    Set objUser = GetObject("LDAP://<SID="& normalizedSID & ">")
    FindUser=objUser.EmailAddress
End Function

希望对其他人有用。

问题回答

暂无回答




相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签