none
Questions about split ..trying to extract 2 levels of OUs from distinguishedname RRS feed

  • Question

  • Hallo .. I'm still on a voyage of self-discovery to teach myself Powershell. Be kind. 

    I'd like to extract two levels of OU names from a computer object's distinguished name in AD for a report. I'm getting .distinguishedname from get-adcomputer.

    I have this:  

            CN=COMPUTERNAME,OU=OU2,OU=OU1,OU=NOTWANTED,OU=NOTTHISEITHER,DC=FOO,DC=BAR,DC=COM

    ...and I'd like to pull out: 

           OU1
           OU2

    I'd like each assigned to its own variable. Through messing around I've been able to get a little of what I want using split:

           $OU = ($blah.DistinguishedName -split ",*..=")[2,3]

    This gives me both OU1 and OU2, but I can't figure out how to separate *that* into two separate values which I can assign to $OU1 & $OU2. 

    Is split even the right choice, or would a regular expression be the way to go? I confess I don't really understand those yet. 

    Examples would be welcome.

    Thanks much. 
    LW



    Lanwench ** Do not fold, spindle or mutilate. Fill in the box completely and erase any stray marks. Use only a #2 pencil.

    Tuesday, April 22, 2014 5:20 PM

Answers

  • This is my preferred method because it eliminates ambiguities.

    $obj=[adsi]"LDAP://$($blah.DistinguishedName)"
    $container1=[adsi]$obj.Parent
    $container2=[adsi]$ou2.Parent
    
    $ou1=$container1.Name
    $ou2=$container2.Name
    
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by Lanwench Tuesday, April 22, 2014 5:50 PM
    Tuesday, April 22, 2014 5:35 PM

All replies

  • $cn,$ou1,$ou2,$extra=$blah.DistinguishedName -split ','


    ¯\_(ツ)_/¯

    Tuesday, April 22, 2014 5:24 PM
  • This is my preferred method because it eliminates ambiguities.

    $obj=[adsi]"LDAP://$($blah.DistinguishedName)"
    $container1=[adsi]$obj.Parent
    $container2=[adsi]$ou2.Parent
    
    $ou1=$container1.Name
    $ou2=$container2.Name
    
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by Lanwench Tuesday, April 22, 2014 5:50 PM
    Tuesday, April 22, 2014 5:35 PM
  • Thanks for the speedy reply, jrv. 

    With your first example, it does split things out, but then I've got 

    OU=OU1

    OU=OU2 

    (and I don't know how to get rid of the 'OU=')

    With the 2nd answer, it didn't work at first, but then I realized you meant to type

    $container2=[adsi]$container1.Parent

    ...and this all works great. Thank you!  My education proceeds apace. 


    Lanwench ** Do not fold, spindle or mutilate. Use only a #2 pencil. Fill in the box completely and erase any stray marks.

    Tuesday, April 22, 2014 5:50 PM
  • $cn,$ou1,$ou2,$extra=$blah.DistinguishedName -split ','

    $x,$ou1name=$ou1 -split '='

     


    ¯\_(ツ)_/¯

    Tuesday, April 22, 2014 5:59 PM
  • Thanks for the speedy reply, jrv. 

    With your first example, it does split things out, but then I've got 

    OU=OU1

    OU=OU2 

    (and I don't know how to get rid of the 'OU=')

    With the 2nd answer, it didn't work at first, but then I realized you meant to type

    $container2=[adsi]$container1.Parent

    ...and this all works great. Thank you!  My education proceeds apace. 


    Lanwench ** Do not fold, spindle or mutilate. Use only a #2 pencil. Fill in the box completely and erase any stray marks.

    Many mysterious methods.

    Isn't it spelled Apache?  Maybe you mean "in space"
    ;)


    ¯\_(ツ)_/¯

    Tuesday, April 22, 2014 6:01 PM
  • Nyuk. That's what you get when the liberal-arts majors go into tech!

    Following your logic I figured out how to add $OU3 also. Goody.

    Is it permissible to ask one more followup question in here or will I be beaten soundly? I'll risk it...

    So, I can now see that although *most* of our servers have a consistent number of 'parent' OUs, some don't. Meaning, it could be one of the following...

    CN=COMPUTERNAME,OU=OU3,OU=OU2,OU=OU1,OU=NOTWANTED,OU=NOTTHISEITHER,DC=FOO,DC=BAR,DC=COM
    CN=COMPUTERNAME,OU=OU2,OU=OU1,OU=NOTWANTED,OU=NOTTHISEITHER,DC=FOO,DC=BAR,DC=COM

    CN=COMPUTERNAME,OU=OU1,OU=NOTWANTED,OU=NOTTHISEITHER,DC=FOO,DC=BAR,DC=COM

    ...and I want to see *only* the OUs that are under OU=NOTWANTED. Children, I guess. 

    I tried filtering - 

    $container3=[adsi]$container2.Parent | | where-object {$_ -notlike "OU=NOTWANTED" }

    and that didn't work at all. No error, but I'm seeing NOTWANTED as part of my output.

    What is one of the many things I am probably doing wrong? 


    Lanwench ** Do not fold, spindle or mutilate. Use only a #2 pencil. Fill in the box completely and erase any stray marks.

    Tuesday, April 22, 2014 6:33 PM
  • Try opening a new question and ask a question. What is it that you are tying to do.  You are trying to force a complex answer out of your limited knowledge. It is better to as "How do I blah blah" and don't use any technical language or code examples. Most times what you are trying to do ha a very simple solution.

    ¯\_(ツ)_/¯

    Tuesday, April 22, 2014 6:52 PM
  • Got it! Thanks.

    Lanwench ** Do not fold, spindle or mutilate. Use only a #2 pencil. Fill in the box completely and erase any stray marks.

    Tuesday, April 22, 2014 7:00 PM