Need help translating Powershell -> Javascript for LDAP (dn -> canonicalName) conversion function

tfrancois

I found a great function that takes a valid Active Directory LDAP distinguishedName (DN) string and converts it to properly formatted canonicalName (not CN) string written in PowerShell.

Because I am using ldapjs in Node.js and need to retrieve an AD object's canonicalName attribute (which is not available natively using any of the Node/AD/LDAP libraries because the attribute in question is "constructed"), if someone can help me convert this function to pure Javascript this would solve my dilemma perfectly.

Anyone willing to take a stab at it? The original post where this code (inserted below) was referenced from can be found here: https://gallery.technet.microsoft.com/scriptcenter/Get-CanonicalName-Convert-a2aa82e5

An example input value:

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

Expected Result:

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

(Hints: Resultant JS function should be able to handle deeply OU-nested objects! Also I'm guessing that RegEx expressions could probably help handle some parts of this pretty nicely but don't have the faintest clue how to implement that.)

Warmest thanks in advance to anyone who can help solve my issue!

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 '/') 
    } 
}
tfrancois

Here a stab at answering my own question, hope it helps someone. Seems to work for me just fine even in the case where the distinguishedName attribute of the user has multiple CNs in the value/path.

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); 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Need help translating into an arrow function

From Dev

Need help translating this JQuery code back to raw Javascript

From Dev

Need help refactoring a javascript function

From Dev

Need help translating text in my ts files

From Dev

Need Help on Translating C# to F#

From Dev

Need help translating a TSQL UNION to a JOIN

From Dev

Need help translating C# into VB

From Dev

Translating Javascript Function to Python

From Dev

Need help for installing LDAP in unix

From Dev

Javascript function in vapor 3 leaf - Help Need

From Dev

Need help understanding function invocation in JavaScript

From Dev

Need help to isolate snippet into function in JavaScript

From Dev

Need help creating a button to activate a javascript function

From Dev

Need help in JavaScript Time and Date Function

From Dev

UnboundID LDAP Java SDK - need to list descendants of a given parent DN

From Javascript

Need some help translating a get value in a JSON file

From Dev

Need help translating those two Cypher queries into Traversal Framework

From Dev

need help translating a code snippet from java to C# equivalent

From Dev

I need help "translating" this code from Godot 3 to Godot 4

From Dev

Need help in XSLT for timezone conversion

From Dev

ldap queries - need the dn of the users who are authenticated via ldap protocol and their IP address

From Dev

Need help on NTLM signing over LDAP authentication

From Dev

Need help for clicking a function

From Dev

Need help in FORMAT function

From Dev

Need help to understand this function

From Dev

Need help writing a function

From Dev

Need Help - First Function

From Dev

Is LDAP DN case insensitive?

From Dev

I need help adding a button to run a function in my JavaScript

Related Related

  1. 1

    Need help translating into an arrow function

  2. 2

    Need help translating this JQuery code back to raw Javascript

  3. 3

    Need help refactoring a javascript function

  4. 4

    Need help translating text in my ts files

  5. 5

    Need Help on Translating C# to F#

  6. 6

    Need help translating a TSQL UNION to a JOIN

  7. 7

    Need help translating C# into VB

  8. 8

    Translating Javascript Function to Python

  9. 9

    Need help for installing LDAP in unix

  10. 10

    Javascript function in vapor 3 leaf - Help Need

  11. 11

    Need help understanding function invocation in JavaScript

  12. 12

    Need help to isolate snippet into function in JavaScript

  13. 13

    Need help creating a button to activate a javascript function

  14. 14

    Need help in JavaScript Time and Date Function

  15. 15

    UnboundID LDAP Java SDK - need to list descendants of a given parent DN

  16. 16

    Need some help translating a get value in a JSON file

  17. 17

    Need help translating those two Cypher queries into Traversal Framework

  18. 18

    need help translating a code snippet from java to C# equivalent

  19. 19

    I need help "translating" this code from Godot 3 to Godot 4

  20. 20

    Need help in XSLT for timezone conversion

  21. 21

    ldap queries - need the dn of the users who are authenticated via ldap protocol and their IP address

  22. 22

    Need help on NTLM signing over LDAP authentication

  23. 23

    Need help for clicking a function

  24. 24

    Need help in FORMAT function

  25. 25

    Need help to understand this function

  26. 26

    Need help writing a function

  27. 27

    Need Help - First Function

  28. 28

    Is LDAP DN case insensitive?

  29. 29

    I need help adding a button to run a function in my JavaScript

HotTag

Archive