Liang Cui
Liang Cui

Reputation: 131

How to prevent infinite loop for recursion Function

I am writing a custom function to get active directory group members. The reason I write it but not use Get-ADGroupMember or Get-QADGroupMember because I want to track the relations between the nested groups within nested groups. The standard function doesn't have it.

Function Get-GrpMmbr {
    param([string]$Identity, [string]$Domain)
    $Members = Get-ADGroupMember -Identity $Identity -Server $Domain
    # Pipe through all members. If it is group, then dump info and call itself;
    # Else (not group), dump info.
    $Members | foreach {
        if ($_.objectClass -eq 'group') {
            # Get the groupName and domain name
            $DistinguishedName = $_.distinguishedName
            $Parts = $DistinguishedName -split ","
            foreach ($Part in $parts) {
                if($Part -like "CN=*"){$GroupName = ($Part -split "=")[1]}
                if($Part -like "DC=*"){$DomainName=($Part -split "=")[1];break}
            }
            [PSCustomObject]@{
                'UserName' = $_.name
                'UserID' = $_.SamAccountName
                'GroupName' = $Identity ## show the group from the direct parent group
                'Type'= "Group"
            } # end of new object
            # recursion happens here
            Get-EFSGrpMmbr -Identity $GroupName -Domain $DomainName
        } else {
            [PSCustomObject]@{
                'UserName' = $_.name
                'UserID' = $_.SamAccountName
                'GroupName' = $Identity
                'Type' = "User"
            } # end of new object
        } # end of Else
    } # end of foreach loop
} # end of function

Problem: it gets into infinite loop with following scenario:

Get-GrpMmbr -Identity 'GroupA' -Domain 'NW'

Condition is: GroupA is member of GroupB; GroupB is member of GroupC; GroupC is member of GroupA.

So, how to stop the infinite loop?

Upvotes: 0

Views: 684

Answers (1)

iRon
iRon

Reputation: 23703

Function Get-GrpMmbr {
    param([string]$Identity, [string]$Domain, [array]$Path = @())
    If ($Path -contains $Identity) {Return}
    ...
    Get-GrpMmbr $GroupName $DomainName ($Path + $Identity)
    ...
}

And if you want to display the infinite loop:

Function Get-GrpMmbr {
    param([string]$Identity, [string]$Domain, [array]$Path = @())
    $i = [array]::indexof($Path, $Identity)     #In case of a sAMAccountName you might want to ignore the case
    If ($i -ge 0) {
        Write-Host "Inifitive loop:" $Path[$i..999]
        Return
    }
    ...
    Get-GrpMmbr $GroupName $DomainName ($Path + $Identity)
    ...
}

Upvotes: 1

Related Questions