Expand Object[] within PSCustomObject

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Michael Delaney Michael Delaney 6 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #36634
    Profile photo of Michael Delaney
    Michael Delaney
    Participant

    I have a script that is connecting to a Novell eDirectory (yes, I know, eDirectory) in order to retrieve user information.

    I am using System.DirectoryServices.DirectoryEntry and System.DirectoryServices.DirectorySearcher. The searching is working well.

    One of the fields I am retrieving from Novell eDirectory is the user's group membership. I am able get the membership, format the entries the way I want without issue. The problem I am having is when I am displaying the information.

    I am using the following code:


    Add-Type -AssemblyName System.DirectoryServices

    #Setup eDirectory Connection Variables
    $eDirPath = "LDAP://SomeNovellServer/o=SomeOrg"
    $eDIrAuthType = 'None'

    #Establish eDirectory Connection and Enumerate
    $Root = New-Object System.DirectoryServices.DirectoryEntry -argumentlist $eDirPath,$null,$null,$eDirAuthType
    $Query = New-Object System.DirectoryServices.DirectorySearcher
    $Query.SearchRoot = $Root

    $Query.Filter = "(uid=miked)"
    $SearchResults = $Query.FindAll()
    $prop = [pscustomobject]@{
    "Name" = $SearchResults.Properties.fullname
    "NovellGroupMembership" = ($SearchResults.Properties.GroupMembership.ForEach({((($_ -split ",") -replace "(ou=)|(o=)",".") -replace "cn=").ToUpper() -join ""}) | Sort-Object)
    }
    Write-Output -InputObject $prop

    Output looks like this

    Name NovellGroupMembership
    ——- ——————————-
    Mike Delaney {GROUP1.SOMEORG, GROUP2.SOMEORG, GROUP3.SOMEORG...}

    This works...but the NovellGroupMembership property when Write-Output sends it to the screen, it is enclosed in in { }. I did pipe $prop to the Get-Member cmdlet, and it returns that NovellGroupMembership is a NoteProperty whose definition is Object[] NovellGroupMembership=System.Object[]

    Ultimately, I could then pipe it to Select-Object -ExpandProperty NovellGroupMembership, but I would like it to display like this

    Name NovellGroupMembership
    ——- ——————————-
    Mike Delaney GROUP1.SOMEORG
    GROUP2.SOMEORG
    GROUP3.SOMEORG
    GROUP4.SOMEORG
    GROUP5.SOMEORG

    Is that possible?

    Windows 7 SP1, PowerShell 5.0.10586.117

    #36636
    Profile photo of random commandline
    random commandline
    Participant

    You can export membership to text or csv file. If you prefer to display name and groups, use this.

    write-output $prop.name $prop.NovellGroupMembership
    
    #36638
    Profile photo of Anthony Stringer
    Anthony Stringer
    Participant

    try using this line instead

    "NovellGroupMembership" = ($SearchResults.Properties.GroupMembership.ForEach({((($_ -split ",") -replace "(ou=)|(o=)",".") -replace "cn=").ToUpper() -join ""}) | Sort-Object) -join "`n"
    #36653
    Profile photo of Michael Delaney
    Michael Delaney
    Participant

    @random commandline,

    Thanks for the reply! That works but it doesn't keep everything in an object, which is my goal in this case.

    @Anthony Stringer
    That's the ticket! I do have to pipe the results of Write-Output to Format-Table with the -Wrap parameter to get everything to display on it's own line. But that's minor.

    Thanks to everyone who replied 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.