-1

我发现了一个很棒的函数,它采用有效的 Active Directory LDAP distinctName (DN) 字符串并将其转换为用 PowerShell 编写的格式正确的 canonicalName(不是 CN)字符串。

因为我在 Node.js 中使用 ldapjs 并且需要检索 AD 对象的 canonicalName 属性(使用任何 Node/AD/LDAP 库本机都无法使用该属性,因为所讨论的属性是“构造的”),所以最好我将此函数转换为纯 JavaScript。

我怎么能刺破它?可以在此处找到引用此代码(在下面插入)的原始帖子: https ://gallery.technet.microsoft.com/scriptcenter/Get-CanonicalName-Convert-a2aa82e5

示例输入值:

'CN=Eric Paw,OU=Sales,OU=People,DC=example,DC=com'

预期结果:

'example.com/People/Sales/Eric Paw'

(提示:结果 JS 函数应该能够处理深度 OU 嵌套的对象!另外我猜测 RegEx 表达式可能有助于很好地处理其中的某些部分,但没有最微弱的线索如何实现它。)

提前向任何可以帮助解决我的问题的人表示最热烈的感谢!

function Get-CanonicalName ([string[]]$DistinguishedName) { 
    foreach ($dn in $DistinguishedName) {      
        ## Split the dn string up into it's constituent parts 
        $d = $dn.Split(',') 
        
        ## get parts excluding the parts relevant to the FQDN and trim off the dn syntax 
        $arr = (@(($d | Where-Object { $_ -notmatch 'DC=' }) | ForEach-Object { $_.Substring(3) }))  
        
        ## Flip the order of the array. 
        [array]::Reverse($arr)  
 
        ## Create and return the string representation in canonical name format of the supplied DN 
        "{0}/{1}" -f  (($d | Where-Object { $_ -match 'dc=' } | ForEach-Object { $_.Replace('DC=','') }) -join '.'), ($arr -join '/') 
    } 
}
4

2 回答 2

1

这并不能回答您的确切问题,但可以回答您的问题。

任何 LDAP 客户端都可以使用构造的属性——您只需要专门要求它们。

例如,如果您正在执行搜索,您可以指定要返回的属性。如果您不指定任何内容,它将返回所有具有值的非构造属性。如果需要构造属性,则需要指定它。

我不知道您使用的是哪个库,但如果您使用的是LDAPjs,则对象中有一个attributes属性options会传递给该search方法。,您可以在其中指定要返回的属性。您可以canonicalName在此处指定(以及您想要的其他属性)。

同样,如果您直接绑定到特定对象,通常有一种方法可以检索构造的属性。

于 2019-01-29T12:50:39.627 回答
0

在这里尝试回答我自己的问题,希望对某人有所帮助。即使在用户的 distinctName 属性在值/路径中有多个 CN 的情况下,似乎也对我有用。

function formatCanonicalName( DN ) {

  var CN = "", DC = "", OU = "";
  DN = DN.replace("\\,", "~");
  DN.split(",").forEach(function(item) {
    switch (item.substring(0,2)) {
      case 'CN': 
        if (item.indexOf('~') > -1) { item = item.split("~")[1] + " " + item.split("~")[0]; }
        CN = item.replace("CN=", "") + "/" + CN; 
        break;
      case 'OU': 
        OU = item.replace("OU=", "") + '/' + OU; 
        break;
      case 
        'DC': DC = DC + item.replace("DC=", "") + '.'; 
        break;
    };
  }); 
  return DC.substring(0, DC.length - 1) + '/' + OU + CN.substring(0, CN.length - 1); 
}
于 2019-02-01T11:03:27.507 回答