【问题标题】:Get NT style domain\user given DN获取 NT 样式域\用户给定的 DN
【发布时间】:2009-11-25 11:44:34
【问题描述】:

我在 Active Directory 中有一个用户的 DN,我想从中获取“NT 风格”域\用户。 sAMAccountname AD 属性给了我用户部分,但是域呢?

谢谢

【问题讨论】:

    标签: active-directory dns


    【解决方案1】:

    您可以通过取用户DN的最后一部分(DC=domain,DC=local)并在前面添加CN=Partitions,CN=Configuration,来获取它。

    然后以CN=Partitions, CN=Configuration, DC=domain, DC=local为起点对(&(nCName="DC=domain,DC=local")(nETBIOSName=*))进行子树搜索;您返回的条目将在 nETBIOSName-attribute 中包含域的 NETBIOS 名称。

    【讨论】:

    • 谢谢,看起来不错。但是,我保证这次搜索只能得到一个值?
    • 如果您的 forrest 中有多个域,则需要在搜索中包含您要搜索的域;我已修改我的回复以考虑到这一点。
    【解决方案2】:

    如何使用 --> System.Security.Principal.NTAccount.ToString()

    在此处查看有关它的 msdn 信息:NTAccount.ToString()

    这应该返回一个域\用户格式的字符串...这是你想要的吗?

    【讨论】:

      【解决方案3】:

      进行此转换的最简单方法是通过DsCrackNames API。您指定输入格式和输出格式,它会为您进行转换。

      【讨论】:

        【解决方案4】:

        这是 PowerShell 代码:

        $hash = @{} //this contains the map of CN and nCNAME
        $Filter = '(nETBIOSName=*)'
        $RootOU = "CN=Partitions,CN=Configuration,DC=DOMAIN,DC=LOCAL" //Change this to your org's domain
        $Searcher = New-Object DirectoryServices.DirectorySearcher
        $Searcher.SearchScope = "subtree"
        $Searcher.Filter = $Filter
        $Searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$($RootOU)")
        $Searcher.FindAll()|sort | foreach { $hash[($_.Properties.ncname).Trim()] = ($_.Properties.cn).Trim() }
        $hash.GetEnumerator() | sort -Property Value
        

        如果$userDetails 中提供了用户详细信息,那么您可以通过以下方式获取正确的域:

        $hash[[regex]::Match($userDetails.DistinguishedName, 'DC=.*').Value]
        

        最终的用户名如下所示:

        $hash[[regex]::Match($userDetails.DistinguishedName, 'DC=.*').Value] + "\" + $userDetails.SamAccountName
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多