我发现了一个很棒的函数,它采用有效的 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 '/')
}
}