Expand Object[] within PSCustomObject

This topic contains 3 replies, has 3 voices, and was last updated by  Michael Delaney 2 years, 3 months ago.

  • Author
  • #36634

    Michael Delaney

    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
    ——- ——————————-

    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

    Is that possible?

    Windows 7 SP1, PowerShell 5.0.10586.117

  • #36636

    random commandline

    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

    Anthony Stringer

    try using this line instead

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

    Michael Delaney

    @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 🙂

You must be logged in to reply to this topic.