【问题标题】:Find Users E-Mail via SID using VBScript and Active Directory使用 VBScript 和 Active Directory 通过 SID 查找用户电子邮件
【发布时间】:2023-04-06 21:37:02
【问题描述】:

我正在解析有关 Windows 系统上用户帐户更改的日志消息。 我想通知用户有关更改,因此我需要检索他们的个人信息 来自 Active Directory 的信息(First、Last、E-Mail)。

我已经找到了检索用户名的方法,但只能通过 WMI 而不是 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

它工作正常,但我想通过 Active Directory 而不是通过 WMI 来完成。 你能帮帮我吗?

【问题讨论】:

    标签: windows vbscript active-directory adsi


    【解决方案1】:

    好的。我找到了一种通过 Active Directory 执行此操作的方法。 这里的completeness是代码:

    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
    

    希望这对其他人有用。

    【讨论】:

    • 我怎样才能让它工作?我需要导出事件日志吗?如果是这样,我怎样才能使脚本通过导出运行。对不起,我不是程序员。我只是尝试将事件中的 SID 与其用户匹配。
    • 我用这个:sql-und-xml.de/freeware-tools/index.html#evt-Watch 不幸的是,它是由德国开发人员编写的,但你可能很幸运使用谷歌翻译 ;-)
    最近更新 更多