powershellreplaceactive-directoryline-breaksdistinguishedname

How do I isolate an AD Distinguished Name, and get rid of extra lines?


I'm trying to print just the first level(?) distinguished name of a person's AD group. A lot of people are part of many groups, and so far, with the groups listed on multiple lines, I've used -replace to get rid of everything except the thing I want.
As it stands, let's say these are the groups John Smith is in:

PS C:\> $member = Get-ADUser jsmith -prop MemberOf | Select Name,MemberOf  
$member.Name  
$member.MemberOf  
Smith, John A - (jsmith)  
CN=VPN Users,OU=Resource,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc  
CN=ResG-Smith,OU=Smith,OU=Research,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc  
CN=Faculty,OU=Business,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc  

My end goal is something like this:

PS C:\> Get-ResearchGroup jsmith  
Smith, John A - (jsmith)  
Smith

Or maybe even:

Name                     MemberOf  
----                     --------  
Smith, John A - (jsmith) Smith

(maybe with some Add-Member jazz, which I'm pretty sure I can figure out on my own)

Anyway, the point is to get 'Smith' by itself from CN=ResG-Smith,OU=Smith,OU=Research,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc
Now, our research groups (ResG) are all named the same thanks to a script, I believe, but it would be preferable to replace to get 'Smith' from OU=Smith rather than CN=ResG-Smith, if that's doable.

Function Get-ResearchGroup {
  [CmdletBinding()]
    Param(
    [Parameter(Position=1)]
    [string[]]$Users,

    [switch]$Raw
    )

  Foreach ($user in $Users) {

    $member = Get-ADUser $user -prop MemberOf | Select Name,MemberOf

    # $member
    #printing out the variable right now results as follows:
    # Name                     MemberOf
    # ----                     --------
    # Smith, John A - (jsmith) {CN=VPN Users,OU=Resource,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc, CN=ResG-Smith,OU=Smith,OU=Research,OU=User Groups,OU=Groups,OU=Delegated O...

    #I still don't know a whole lot about objects yet, but I wasn't too surprised when -replace didn't work on my variable. I tried this:
    # $member -replace '.*ResG-Smith,OU=' -replace ',OU=Research,.*'
    #results:
    # @{MemberOf=Microsoft.ActiveDirectory.Management.ADPropertyValueCollection}
    #(and, sure, maybe I just set something up wrong)

    #Now, this kind of works:
    # $member.MemberOf -replace '.*CN=ResG-Smith,OU=' -replace ',OU=Research,.*'
    # CN=VPN Users,OU=Resource,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc
    # Smith
    # CN=Faculty,OU=Business,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc
    #Except only on the applicable line.

    #This is pretty much where I'm at right now. Everything below the stackoverflow link are my failed attempts/additions.
    $member.Name
    $MemberOf = $member.memberof -replace '.*CN=ResG-' -replace ',OU=Research,.*' -replace '.*OU='  -replace 'Delegation,.*' -replace 'Enterprise,.*' -replace 'Groups,.*'
    $MemberOf | where { $_ -ne "" }
    #https://stackoverflow.com/questions/19168475/powershell-to-remove-text-from-a-string

    #I tried some different variations of adding each of these (never more than one at a time) on to the chain of replaces, with no success:
    # -replace " `r`n"
    # -replace " `\r`\n"
    # -replace " \\r\\n"
    # -replace "`r`n"
    # -replace "`\r`\n"
    # -replace "\\r\\n"
    #https://stackoverflow.com/questions/22238465/removing-line-break-powershell
    #I've also tried a few variations of .replace(), which didn't work (same results)

    #https://community.spiceworks.com/topic/825700-get-aduser-distinguishedname-powershell-help
    # Get-ADUser jsmith -prop MemberOf | Select Name,MemberOf,@{l='OU';e={$_.DistinguishedName.split(',')[1].split('=')[1]}
    # The hash literal was incomplete.
    # + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    # + FullyQualifiedErrorId : IncompleteHashLiteral

    #The next code look pretty similar to above, but there appear to be some differences, so I thought I'd try it.
    #source: https://www.experts-exchange.com/questions/28206442/Get-part-of-Distinguished-Name-and-Output-to-file-in-Powershell.html
    # Get-ADUser jsmith -prop MemberOf | Select Name,MemberOf,@{name="Research";expression={($_.DistinguishedName -split ",OU=")[1]}}
    # Name                       MemberOf                                                                                                                                                                                  
    # ----                       --------                                                                                                                                                                                  
    # Smith, John A - (jsmith)   {CN=VPN Users,OU=Resource,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegation,DC=domainetc, CN=ResG-Smith,OU=Smith,OU=Research,OU=User Groups,OU=Groups,OU=Delegated OUs,OU=Delegat...

  }
}

Solution

  • Another way to do this without parsing the DistinguishedName with string methods would be to do a Get-ADGroup lookup. It's less efficient with the second lookup, but less likely to break on unexpected directory paths.

    function Get-ResearchGroup {
        param (
            [string[]]$Identity
        )
        foreach ($ID in $Identity) {
            $User = Get-ADUser $ID -Properties MemberOf
            $ResearchGroupsDN = $User.MemberOf | Where-Object {$_ -like 'CN=ResG*'}
            foreach ($ResearchGroupDN in $ResearchGroupsDN) {
                $ResearchGroupObject = Get-ADGroup $ResearchGroupDN
                [PSCustomObject]@{
                    'Name' = $User.Name
                    'MemberOf' = $ResearchGroupObject.Name -replace 'ResG',''
                }
            }
        }
    }