locked
How to use split to get the part after the first item RRS feed

  • Question

  • Hi Guys

    for example

     $DN = "CN=LQIT010,OU=PCs_Keep_Local_Admins,OU=XXX,DC=corp,DC=bl,DC=com"

    we want to split the $DN and save the part after the first "," to a new variable ,

    like $partnet _DN = "OU=PCs_Keep_Local_Admins,OU=XXX,DC=corp,DC=bl,DC=com"

    may i know how .

    Thanks

    Monday, March 25, 2019 7:17 AM

All replies

  • $dn -split ','


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Monday, March 25, 2019 9:03 AM
    Monday, March 25, 2019 7:31 AM
  • use $dn -split ',‘ will split the $dns to many parts, what i want is extract the part after the first ','---

    "OU=PCs_Keep_Local_Admins,OU=XXX,DC=corp,DC=bl,DC=com"

    Monday, March 25, 2019 8:38 AM
    • Proposed as answer by BOfH-666 Monday, March 25, 2019 9:03 AM
    Monday, March 25, 2019 8:47 AM
  • @{n=’OUPath’;e={$_.distinguishedName -replace '^.+?,(CN|OU.+)’,’$1'}}
    Monday, March 25, 2019 9:29 AM
  • $dn = 'CN=LQIT010,OU=PCs_Keep_Local_Admins,OU=XXX,DC=corp,DC=bl,DC=com'
    $split = $dn -split ','
    $split[1..$split.count] -join ','

    OU=PCs_Keep_Local_Admins,OU=XXX,DC=corp,DC=bl,DC=com
    • Edited by JS2010 Monday, March 25, 2019 2:49 PM
    Monday, March 25, 2019 2:46 PM
  • $dn = "CN=LQIT010,OU=PCs_Keep_Local_Admins,OU=XXX,DC=corp,DC=bl,DC=com"

    $dn.Substring($dn.IndexOf(',OU=')+1)
    OU=PCs_Keep_Local_Admins,OU=XXX,DC=corp,DC=bl,DC=com

    Monday, March 25, 2019 2:58 PM
  • Try this:

    $partnet_DN = ($DN -split ',', 2)[1]


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, March 25, 2019 3:30 PM
  • function Get-DistinguishedNameParent {
        param([string]$DN)
        $DirectoryServices = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices")
        $Utils = $DirectoryServices.GetType("System.DirectoryServices.ActiveDirectory.Utils")
        $MethodInfo = $Utils.GetMethod("GetPartialDN", [Reflection.BindingFlags] "NonPublic, Static")
        $MethodInfo.Invoke($null, @($DN, 1))
    }
    
    Get-DistinguishedNameParent -DN 'CN=User,OU=Users,DC=some,DC=domain'
    
    # Output:
    # OU=Users,DC=some,DC=domain
    
    function Parse-DistinguishedName {
        param([string]$DN)
        $DirectoryServices = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices")
        $Utils = $DirectoryServices.GetType("System.DirectoryServices.ActiveDirectory.Utils")
        $MethodInfo = $Utils.GetMethod("GetDNComponents", [Reflection.BindingFlags] "NonPublic, Static")
        $MethodInfo.Invoke($null, $DN)
    }
    
    Parse-DistinguishedName -DN 'CN=User,OU=Users,DC=some,DC=domain'
    
    # Output:
    # Name Value 
    # ---- ----- 
    # CN   User  
    # OU   Users 
    # DC   some  
    # DC   domain
    
    function Get-RDNFromDistinguishedName {
        param([string]$DN)
        $DirectoryServices = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices")
        $Utils = $DirectoryServices.GetType("System.DirectoryServices.ActiveDirectory.Utils")
        $MethodInfo = $Utils.GetMethod("GetRdnFromDN", [Reflection.BindingFlags] "NonPublic, Static")
        $MethodInfo.Invoke($null, $DN)
    }
    
    Get-RDNFromDistinguishedName -DN 'CN=User,OU=Users,DC=some,DC=domain'
    
    # Output:
    # CN=User


    Monday, March 25, 2019 5:05 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, March 27, 2019 6:49 AM