Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-NestedMembership {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
- [object[]] $InputObject
- )
- Begin {
- $users = New-Object System.Collections.Generic.List[object]
- #region INNER FUNCTION
- Function getRecursiveMembers {
- param (
- [System.Collections.Generic.HashSet[string]]$Processed,
- [string]$DistinguishedName
- )
- $users = New-Object 'System.Collections.Generic.List[object]'
- $ldapFilter = "(memberOf=$DistinguishedName)"
- [array] $objs = Get-ADObject -LDAPFilter $ldapFilter
- foreach ($member in $objs) {
- if ($member.ObjectClass -eq "group" -and $Processed.Add($member.DistinguishedName)) {
- $innerResult = getRecursiveMembers -Processed $Processed -DistinguishedName $member.DistinguishedName
- $Processed.UnionWith($innerResult.Processed)
- $users.AddRange($innerResult.Users)
- }
- else {
- $member.psobject.Properties.Add((New-Object PSNoteProperty("FromGroup", $DistinguishedName)))
- $users.Add($member)
- }
- }
- [pscustomobject]@{
- Users = $users
- Processed = $Processed
- }
- }
- #endregion
- }
- Process {
- foreach ($group in $InputObject) {
- $processed = New-Object System.Collections.Generic.HashSet[string] -ArgumentList 1
- [void] $processed.Add($group.DistinguishedName)
- $result = getRecursiveMembers -Processed $processed -DistinguishedName $group.DistinguishedName
- foreach ($u in $result.Users) {
- $u.psobject.Properties.Add((New-Object PSNoteProperty("MemberOf", $group.Name)))
- }
- $users.AddRange($result.Users)
- }
- }
- End {
- $users
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement