Author Posts

March 16, 2016 at 8:35 am

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

March 16, 2016 at 9:38 am

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

March 16, 2016 at 11:22 am

try using this line instead

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

March 17, 2016 at 6:16 am

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