locked
How do I filter our parts of a string using powershell? RRS feed

  • Question

  • Hi all,

    The goal I'm trying to accomplish is getting the DN of a user's object without any part of the CN= attribute using powershell.

    For example, here's what I get for the DN of a user:

    CN=Smith\, John (jsmith),OU=NewYork,OU=Users,DC=Example,DC=Com

    I'm having trouble understanding how to filter out the text in the string in the beginning, which is the part that contains:

    CN=Smith\, John (jsmith), 

    All I want from the string is the OU=NewYork part.  I can handle stripping out the ,OU=Users,DC=Example,DC=Com using the replace function.  If all of my users had the same number of characters for their name, that would be easy.  But alas, that is obviously not the case.

    The problem is that I have a csv file with many users, so the "prefix" where the CN= part is always going to be some variable length.  My idea was to somehow search the string for a match of  ), and then remove all text before that match.  But I don't know how to and apparently don't know the proper terminology to use to search on.

    Any help would be greatly appreciated.

    Tuesday, July 15, 2014 8:18 PM

Answers

  • Thanks for the reply, but that returns the inverse of what I'm looking for. Is there a way to invert your solution? To carlify, your solution provided the CN= part. That's that part that I do NOT want. I want everything else, with the OU=,DC= parts.

    $dn='CN=Smith\, John (jsmith),OU=NewYork,OU=Users,DC=Example,DC=Com'
    ([adsi]"LDAP://$dn").Parent

    Returns the container housing the object.


    ¯\_(ツ)_/¯

    Tuesday, July 15, 2014 8:34 PM

All replies

  • This is the easiest and most reliable way:

    $dn='CN=Smith\, John (jsmith),OU=NewYork,OU=Users,DC=Example,DC=Com'
    ([adsi]"LDAP://$dn").Name


    ¯\_(ツ)_/¯

    Tuesday, July 15, 2014 8:24 PM
  • Thanks for the reply, but that returns the inverse of what I'm looking for. Is there a way to invert your solution? To carlify, your solution provided the CN= part. That's that part that I do NOT want. I want everything else, with the OU=,DC= parts.
    Tuesday, July 15, 2014 8:29 PM
  • I would recommend using the Pathname object like Get-ADPathname.ps1 in this article:

    Windows IT Pro: Use PowerShell to Handle Active Directory Paths

    Example:


    PS C:\> $dn = "CN=Smith\, John (jsmith),OU=NewYork,OU=Users,DC=Example,DC=Com"
    PS C:\> Get-ADPathname $dn -GetElement 0
    # Outputs "CN=Smith\, John (jsmith)"
    

    Bill


    -- Bill Stewart [Bill_Stewart]


    Tuesday, July 15, 2014 8:30 PM
  • Thanks Bill. I'll give that a shot if I can't find a native PowerShell solution.
    Tuesday, July 15, 2014 8:32 PM
  • The Get-ADPathname.ps1 script is just a wrapper around the Pathname COM object, so it really is a "native" solution. You don't need the script if you want to use the Pathname directly (it's rather ugly but can be done); the script is just to make your life easier.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 15, 2014 8:34 PM
  • Thanks for the reply, but that returns the inverse of what I'm looking for. Is there a way to invert your solution? To carlify, your solution provided the CN= part. That's that part that I do NOT want. I want everything else, with the OU=,DC= parts.

    $dn='CN=Smith\, John (jsmith),OU=NewYork,OU=Users,DC=Example,DC=Com'
    ([adsi]"LDAP://$dn").Parent

    Returns the container housing the object.


    ¯\_(ツ)_/¯

    Tuesday, July 15, 2014 8:34 PM
  • -Replace will work if you use a "lazy" match at the beginning of the string to prevent trimming off any of the OU's:

    $dn = 'CN=Smith\, John (jsmith),OU=NewYork,OU=Users,DC=Example,DC=Com'
    $dn -replace '^(?:.+?),(OU.+)','$1'
    
    OU=NewYork,OU=Users,DC=Example,DC=Com


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, July 15, 2014 8:35 PM
  • Thanks jrv, that did it!
    Tuesday, July 15, 2014 8:35 PM
  • If you don't want the path use this:

    ([adsi]"LDAP://$dn").Parent.Replace('LDAP://','')


    ¯\_(ツ)_/¯

    Tuesday, July 15, 2014 8:40 PM
  • Watch out for characters that require escaping (that aren't already escaped in the path you pass to it).


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 15, 2014 8:40 PM