none
Powershell splitting RRS feed

  • Question

  • Hi,

    I have one output for distinguished name as :

    CN=OSDWIN7SOE,OU=Laptops,OU=ABC Data Centre,OU=ABC - Mumbai,OU=ABC - India,DC=INDIAUAT,DC=ABCUAT,DC=COM

    I need a powershell code to only retrieve the CN name that is my output should have only : OSDWIN7SOE.

    Help me with this. 

    Thanks in advance

    Thursday, February 6, 2014 1:23 PM

Answers

All replies

  • Hi theAbyKumar,

    this is a fairly simple task, so it looks like you could use some help with learning basic string editing.
    Soo ... here is a good tutorial for that.

    That done, every string has the function .Split(), using it you can turn a string into a string-array, split at the string-values you put into the brackets after string (For example $s = $dn.Split(",")).

    Once split, you can select individual parts by their number ($s[0] returns the first segment, $s[1] the second, $s[2] the third, and so on).

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 6, 2014 1:58 PM
  • The only downside to that approach is that it will break if there's an escaped comma in the CN. For example, "CN=Wyatt\, Dave,OU=etc..." is a legal distinguished name, but a simple split on the comma will fail. Here's how you can get around that using regex in PowerShell's -split operator:

    $dn = "CN=Wyatt\, Dave,OU=SomeOU,DC=domain,DC=com"
    
    $cn = ($dn -split '(?<!\\),')[0]
    
    $cn

    This isn't 100% perfect, either; technically, it's possible that the backslash before the comma was itself escaped, but that's pretty rare. 

    Thursday, February 6, 2014 2:08 PM
  • How are you retrieving the distinguished name in the first place.

    Is this from another PowerShell command, or from an input file?

    If its from another PowerShell command, then you should be able to include the CN attribute earlier in the pipe.



    Thursday, February 6, 2014 2:09 PM
  • The only downside to that approach is that it will break if there's an escaped comma in the CN. For example, "CN=Wyatt\, Dave,OU=etc..." is a legal distinguished name, but a simple split on the comma will fail. Here's how you can get around that using regex in PowerShell's -split operator:

    $dn = "CN=Wyatt\, Dave,OU=SomeOU,DC=domain,DC=com"
    
    $cn = ($dn -split '(?<!\\),')[0]
    
    $cn

    This isn't 100% perfect, either; technically, it's possible that the backslash before the comma was itself escaped, but that's pretty rare. 

    Come to think of it, you could also split it by ",OU=", that ought to work fairly reliable as well, given that only the first CN is wanted ...

    There's no place like 127.0.0.1

    Thursday, February 6, 2014 2:29 PM
  • $CN=(Get-ADObject "CN=OSDWIN7SOE,OU=Laptops,OU=ABC Data Centre,OU=ABC - Mumbai,OU=ABC - India,DC=INDIAUAT,DC=ABCUAT,DC=COM" -properties CN).CN

    The advantage of this is that you are validating that the object exists.

    Thursday, February 6, 2014 3:15 PM
  • Easiest way and it always works.

    $s='CN=OSDWIN7SOE,OU=Laptops,OU=ABC Data Centre,OU=ABC - Mumbai,OU=ABC - India,DC=INDIAUAT,DC=ABCUAT,DC=COM'
    $obj=[adsi]"LDAP://$s"
    $obj.name


    ¯\_(ツ)_/¯

    Thursday, February 6, 2014 3:41 PM
  • You can also use Get-ADPathname.ps1:


    PS C:\> Get-ADPathname -Path "CN=OSDWIN7SOE,OU=Laptops,OU=ABC Data Centre,OU=ABC - Mumbai,OU=ABC -
     India,DC=INDIAUAT,DC=ABCUAT,DC=COM" -GetElement 0 -ValuesOnly
    OSDWIN7SOE

    Bill

    Thursday, February 6, 2014 3:53 PM
    Moderator